Android全局异常统一处理
16lz
2021-12-04
全局异常处理
主要用到的类:
1. BaseException extends Exception
2. ExceptionHandler
BaseException
public class BaseException extends Exception { public BaseException() { super(); } public BaseException(String detailMessage) { super(detailMessage); } public BaseException(String detailMessage, Throwable throwable) { super(detailMessage, throwable); } public BaseException(Throwable throwable) { super(throwable); } }
ExceptionHandler
public class ExceptionHandler { /** * 根据不同的Exception给用户具体的提示 * * @param context * @param e */ public static void showException(Context context, BaseException e) { int errorCode = 0; if (e instanceof ExceptionA) { errorCode = 1; } else if (e instanceof ExceptionB) { errorCode = 2; } else if (e instanceof ExceptionC) { errorCode = 3; } else if (e instanceof ExceptionD) { errorCode = 4; } //根据不同的错误代码进行相应的错误提示 showExceptionByCode(context, errorCode); } private static void showExceptionByCode(Context context, int errorCode) { String content = ""; switch (errorCode) { case 1: content = "程序异常ExceptionA"; break; case 2: content = "程序异常ExceptionB"; break; case 3: content = "程序异常ExceptionC"; break; case 4: content = "程序异常ExceptionD"; break; default: break; } Toast.makeText(context, content, Toast.LENGTH_SHORT).show(); } }
所有的方法抛出的异常默认继承自BaseException.
public class Methods { /** * 模拟出现ExceptionA * @throws ExceptionA */ public void methodA() throws ExceptionA { throw new ExceptionA(); } /** * 模拟出现ExceptionB * @throws ExceptionB */ public void methodB() throws ExceptionB { throw new ExceptionB(); } /** * 模拟出现ExceptionC * @throws ExceptionC */ public void methodC() throws ExceptionC { throw new ExceptionC(); } /** * 模拟出现ExceptionD * @throws ExceptionD */ public void methodD() throws ExceptionD { throw new ExceptionD(); } }
测试:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_1; private Button btn_2; private Button btn_3; private Button btn_4; private Methods methods; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); methods = new Methods(); btn_1 = (Button) findViewById(R.id.button); btn_2 = (Button) findViewById(R.id.button2); btn_3 = (Button) findViewById(R.id.button3); btn_4 = (Button) findViewById(R.id.button4); btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); btn_4.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button: try { methods.methodA(); } catch (BaseException exception) { ExceptionHandler.showException(this,exception); } break; case R.id.button2: try { methods.methodB(); } catch (BaseException exception) { ExceptionHandler.showException(this,exception); } break; case R.id.button3: try { methods.methodC(); } catch (BaseException exception) { ExceptionHandler.showException(this,exception); } break; case R.id.button4: try { methods.methodD(); }catch (BaseException exception) { ExceptionHandler.showException(this,exception); } break; } } }
流程图:
更多相关文章
- eclipse 插件网址
- android软件开发:横竖屏
- 16 个Android(安卓)开发者必备工具资源
- android studio在编辑时出现如Failed to sync Gradle project类
- [Android(安卓)TV] 1 初识TV apps
- Ubuntu 13.04 编译环境配置及android 2.3 源代码编译时出现了以
- android打电话,发短信
- 使用天天模拟器开发Android应用
- android 调试方法