I am confused about the behaviour of the CustomExceptions in below program.If Line2 is commented and Line1 is not then program works well,but if Line1 is commented and Line2 is not then compile time error comes as "Unreachable catch block for CustomChecked.This exception is never thrown from the try statement body" for Line3.Please help me why this comple time exception comes only for the unChecked Exception?.
try { if(true) { throw new CustomChecked("Checked Exception");// Line1 // throw new CustomUnChecked("Un-Checked Exception");// Line2 } } catch(CustomChecked ex) //Line3 { System.out.println(ex.getMessage()); } catch(CustomUnChecked ex) { System.out.println(ex.getMessage()); } Exceptions :
class CustomChecked extends Exception { public CustomChecked(String msg) { super(msg); } } class CustomUnChecked extends RuntimeException { public CustomUnChecked(String msg) { super(msg); } }