How to propagate an exception in java

后端 未结 4 1809
無奈伤痛
無奈伤痛 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:15

    Just rethrow the exception

    throw Excp1;

    You will need to add the exception type to the MyMthod() declaration like this

    public void MyMethod() throws ExceptionType1, ExceptionType2  {
    
       try{
       //Some code here which can throw exceptions
       }
       catch(ExceptionType1 Excp1){
           throw Excp1;
       }
       catch(ExceptionType2 Excp2){
           throw Excp2;
       }
    }
    

    Or just omit the try at all since you are no longer handling the exceptions, unless you put some extra code in the catch statements doing things with the exception before rethrowing it.

提交回复
热议问题