Try to catch another method

Try Catch from another method:

method1(){
   try {

       method2();

   }catch(Exception e){


   }
}

 method2(){
    try{

       //ERROR FROM HERE

    }catch(Exception e){

    }

 }

      

How to method1()

catch the error from method2()

?

+3


source to share


4 answers


method1()

will not catch the error if you haven't re-tossed it from block catch

to method2()

.



void method2() {
    try {
        // Error here
    } catch(Exception e) {
        throw e;
    }
}

      

+9


source


If you have thrown another exception in the catch2 block of method2.



public void method2() {
    try {
        // ...
    } catch(Exception e) {
        throw new NullPointerException();
    }
}

      

+2


source


    public void method1(){
        try {
            test2();
        } catch (IOException ex) {
            //catch test2() error
        }
    }

    public void method2() throws IOException{

    }

      

Use throws

+2


source


This won't happen until you drop it into catch

your block by method2

adding throw e;

.

0


source







All Articles