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 = 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;
            }
        }
    }

流程图:

全局异常统一处理流程图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值