Doubt about Try-Catch nested in Java

1

This is a little doubt about the logic of having a Try-Catch inside another Try-Catch in Java, for example it would have something like this:

try{
 //codigo aqui
   try{
     //Aqui ocurre el error
   }catch(Exception ex){
     System.out.print(ex.getMessage() + "inner exception"); //Entra en este catch
   }

}catch(Exception ex){
  System.out.print(ex.getMessage() + "outer exception");
}

As you can see, I put the internal Try-Catch where I think it will give the error, in the Try of the main Try-Catch, my doubt would be, if it happens to give an error in the internal Try-Catch, it would only print the message of the Internal Catch? Or would the external Catch message also be printed? Or would the two be printed?

    
asked by Jason0495 14.11.2018 в 21:34
source

1 answer

1

Only the "Inner exception" message would be printed, since the exception is only within that catch. If you want to make "jump" to the catch outside you have to make a throw, something like this:

try{
 //codigo aqui
   try{
     //Aqui ocurre el error
   }catch(Exception ex){
     System.out.print(ex.getMessage() + "inner exception"); //Entra en este catch
     throw ex;
   }

}catch(Exception ex){
  System.out.print(ex.getMessage() + "outer exception");
}

Good luck!

    
answered by 14.11.2018 / 21:37
source