- All catch blocks must be ordered from most specific to most generic.
- There could be more than one “catch” block but “finally” clause will be at max one.
————————-Illegal code
package technical.jungle.com.exception;
public class ExceptionDemo {
public static void main(String arfs[]) {
int value[] = { 1, 2, 3, 4 };
try {
System.out.println(“..try .. start..”);
System.out.println(“..Array Value value[10] : ” + value[10]);
System.out.println(“..try.. end..”);
}
catch (IndexOutOfBoundsException e) {
e.printStackTrace();
System.out.println(“..IndexOutOfBoundsException – catch..”);
}
catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
System.out.println(“..ArrayIndexOutOfBoundsException – catch..”);
}
catch (RuntimeException e) {
e.printStackTrace();
System.out.println(“..RuntimeException – catch..”);
}
catch (Exception e) {
e.printStackTrace();
System.out.println(“..Exception – catch..”);
}
}
}
———————Legal code
Final solution is:
try {
System.out.println(“..try .. start..”);
System.out.println(“..Array Value value[10] : ” + value[10]);
System.out.println(“..try.. end..”);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println(“..ArrayIndexOutOfBoundsException – catch..”);
}
catch (IndexOutOfBoundsException e) {
System.out.println(“..IndexOutOfBoundsException – catch..”);
}
Output:
..try .. start..
..ArrayIndexOutOfBoundsException – catch..
java.lang.ArrayIndexOutOfBoundsException: 10 at technical.jungle.com.exception.ExceptionDemo.main(ExceptionDemo.java:11)
- Finally block always runs after try catch clause. Repeat after me, finally will run in each and every condition except when JVM shuts down or System.exit() will be invoked somehow.
- Finally block runs whether exception is thrown or not, Finally block runs whether exception is caught or not, means in each and every scenario, finally block will be called.
———————Legal code
package technical.jungle.com.exception;
public class ExceptionDemoFinallyBlock {
public static void main(String arfs[]) {
int value[] = { 1, 2, 3, 4 };
try {
System.out.println(“..try .. start..”);
System.out.println(“..Array Value value[10] : ” + value[10]);
System.out.println(“..try.. end..”);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
System.out.println(“..ArrayIndexOutOfBoundsException – catch..”);
} finally {
System.out.println(“Clean the resource.”);
}
System.out.println(“After finally block”);
}
}
Output:
- It is legal to omit either the catch clause or finally clause. There is no compile time issue in this case. But remember, if any exception occurs then Exception Handler will check for appropriate “catch” block. So better to handle exception with appropriate “catch” block.
public class ExceptionDemoRequiredBlocks {
public static void main(String arfs[]) { try { System.out.println(“..try .. start..”); System.out.println(“..try.. end..”); } finally { System.out.println(“Clean the resource.”); } System.out.println(“No catch block….After finally block”); }}
- “catch” block immediately follow “try” block. and if there is any “finally” block then it should immediately follow “catch” block.
- There should not be unwanted things between try …catch block or catch …finally block. It will give compile time issue.
Great post.