Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try Catching from another Method

Try Catch from another method:

method1(){
   try {

       method2();

   }catch(Exception e){


   }
}

 method2(){
    try{

       //ERROR FROM HERE

    }catch(Exception e){

    }

 }

How will method1() catch the error from method2()?

like image 305
Christian Eric Paran Avatar asked Jul 19 '26 15:07

Christian Eric Paran


2 Answers

method1() will not catch the error, unless you re-throw it from the catch block in method2().

void method2() {
    try {
        // Error here
    } catch(Exception e) {
        throw e;
    }
}
like image 165
Alex DiCarlo Avatar answered Jul 21 '26 05:07

Alex DiCarlo


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

    public void method2() throws IOException{

    }

Use throws

like image 36
Jason Avatar answered Jul 21 '26 03:07

Jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!