天天看點

Android全局異常統一處理全局異常處理

全局異常處理

主要用到的類:

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 = ;
            if (e instanceof ExceptionA) {
                errorCode = ;
            } else if (e instanceof ExceptionB) {
                errorCode = ;
            } else if (e instanceof ExceptionC) {
                errorCode = ;
            } else if (e instanceof ExceptionD) {
                errorCode = ;
            }
            //根據不同的錯誤代碼進行相應的錯誤提示
            showExceptionByCode(context, errorCode);
        }

        private static void showExceptionByCode(Context context, int errorCode) {
            String content = "";
            switch (errorCode) {
                case :
                    content = "程式異常ExceptionA";
                    break;
                case :
                    content = "程式異常ExceptionB";
                    break;
                case :
                    content = "程式異常ExceptionC";
                    break;
                case :
                    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全局異常統一處理全局異常處理