Important facts about try…catch…finally block

  • 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..”);
}
}
}

Conslution
This gives compile time issue. If both IndexOutOfBoundsException and ArrayIndexOutOfBoundsException will be replaced to each other then this issue can be resolved.

———————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:

..try .. start..
..ArrayIndexOutOfBoundsException – catch..
Clean the resource.
After finally block
java.lang.ArrayIndexOutOfBoundsException: 10  at technical.jungle.com.exception.ExceptionDemoFinallyBlock.main(ExceptionDemoFinallyBlock.java:11)
  • 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.
———————Legal code


package technical.jungle.com.exception;
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”); }}
Output:
..try .. start..
..try.. end..
Clean the resource.
After finally block
———————Legal code

package technical.jungle.com.exception;
public class ExceptionDemoRequiredBlocks {
public static void main(String arfs[]) {
try {
System.out.println(“..try .. start..”);
System.out.println(“..try.. end..”);
}
catch(Exception e) {
System.out.println(“.. catch…..”);
}
System.out.println(“No Finally block…..After catch block”);
}
}
Output:
..try .. start..
..try.. end..
After catch 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.
    ————————-Illegal code


    package technical.jungle.com.exception;
    public class ExceptionDemoRequiredBlocks {
    public static void main(String arfs[]) {
    try {
    System.out.println(“..try .. start..”);
    System.out.println(“..try.. end..”);
    }
    System.out.println(“There should not be anything in between”);
    catch(Exception e) {
    System.out.println(“.. catch…..”);
    }
    System.out.println(“There should not be anything in between”);
    finally {
    System.out.println(“.. finally…..”);
    }
    System.out.println(“After finally block”);
    }
    }
    Suggestion:
    Remove print statement which is marked as RED.
    If you like the post then please socialize it. Happy Sharing. Loudly Subscribing.

    One Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *