Android全局异常统一处理
16lz
2021-01-23
全局异常处理
主要用到的类:
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; } } }
流程图:
更多相关文章
- Android控制台中运行Java程序
- 程序员也是会浪漫的->打造浪漫的Android表白程序
- ANDROID – 使用特定的URL开启应用程序
- 说说在 Android 如何在全局获取 Context
- Android点击2次返回按钮退出应用程序
- Android自学笔记:应用程序组件