Java exception handling allows us to use nested 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>
Labels:
Core Java
Exceptions