Nested try-catch block in java


Java exception handling allows us to use nested try-catch block.

Nested try-catch block means using try-catch block inside another try-catch block.



/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ExceptionTest {
   public static void main(String[] args) {
         
          try{
                 int i=10/0; //will throw ArithmeticException
          }catch(ArithmeticException ae){
                 System.out.println("try-catch block handled - ArithmeticException");
                
                 //using nested try-catch block
                 try{
                       String s=null;
                       s.charAt(0); //will throw NullPointerException
                 }catch(NullPointerException npe){
                       System.out.println("NESTED try-catch block handled - "
                                                             + "NullPointerException");
                 }
                
          }
   }
}
/*OUTPUT
try-catch block handled - ArithmeticException
NESTED try-catch block handled - NullPointerException
*/






RELATED LINKS>




eEdit
Must read for you :