How to propagate an exception in java

后端 未结 4 1818
無奈伤痛
無奈伤痛 2020-12-19 00:30

I am a C programmer and just learning some java recently because I am developing one android application. Currently I am in a situation. Following is the one.



        
4条回答
  •  庸人自扰
    2020-12-19 01:03

    Don't catch it and rethrow again. Just do this and catch it in the place you want

    public void myMethod() throws ExceptionType1, ExceptionType2 {
        // other code
    }
    

    Example

    public void someMethod() {
        try {
            myMethod();
        } catch (ExceptionType1 ex) {
            // show your dialog
        } catch (ExceptionType2 ex) {
            // show your dialog
        }
    }
    

提交回复
热议问题