Abhijeet Raj
Abhijeet Raj

Reputation: 3

execute remaining code in try block in case of exception

I have the following code :

try{
  line1; // can throw exception
  line2; // can throw exception
  line3; // can throw exception
  line4; // can throw exception
}catch(Exception e){
   handle exception;
}finally{
  do remaining stuffs;
}

I need to execute all 4 lines inside try block even in case of exception from above lines.

Suppose the code encountered an exception in line2 of try block, even then I need to execute line3 and line4(which in turn can throw exceptions). Is there an easy way to do this instead of having multiple try-catch blocks.

Upvotes: 0

Views: 687

Answers (2)

Holger
Holger

Reputation: 298173

The try with resource statement allows to specify actions that ought be executed even in the exceptional case, with a concise syntax (compared to nested finally blocks). It also has another advantage: when multiple actions fail, the exceptions do not shadow each other but get registered as suppressed throwables at the primary throwable. A drawback is that resources are closed in the opposite order they were created, so we have to reverse the order of the lines:

public static void main(String[] args) {
    for(int test = 0; test < 16; test++) {
        int currentTest = test;
        System.out.println("Test "+currentTest);

        try(AutoCloseable c1 = () -> line4(currentTest);   // 4
            AutoCloseable c2 = () -> line3(currentTest);   // 3
            AutoCloseable c3 = () -> line2(currentTest)) { // 2
            line1(currentTest);                            // 1
        }
        catch(Exception ex) {
            System.out.print("got exception ");
            ex.printStackTrace(System.out);
        }

        System.out.println();
    }
}
public static void line1(int whichTest) {
    if((whichTest & 1) != 0) {
        System.out.println("letting line 1 fail");
        throw new RuntimeException("line 1 failed");
    } else System.out.println("line1 executed");
}
public static void line2(int whichTest) {
    if((whichTest & 2) != 0) {
        System.out.println("letting line 2 fail");
        throw new RuntimeException("line 2 failed");
    } else System.out.println("line2 executed");
}
public static void line3(int whichTest) {
    if((whichTest & 4) != 0) {
        System.out.println("letting line 3 fail");
        throw new RuntimeException("line 3 failed");
    } else System.out.println("line3 executed");
}
public static void line4(int whichTest) {
    if((whichTest & 8) != 0) {
        System.out.println("letting line 4 fail");
        throw new RuntimeException("line 4 failed");
    } else System.out.println("line4 executed");
}

This example program runs through all possible scenarios. I shortened the output to show only some of the examples:

Test 0
line1 executed
line2 executed
line3 executed
line4 executed

Test 1
letting line 1 fail
line2 executed
line3 executed
line4 executed
got exception java.lang.RuntimeException: line 1 failed
    at SafeActions.line1(SafeActions.java:23)
    at SafeActions.main(SafeActions.java:10)
Test 9
letting line 1 fail
line2 executed
line3 executed
letting line 4 fail
got exception java.lang.RuntimeException: line 1 failed
    at SafeActions.line1(SafeActions.java:23)
    at SafeActions.main(SafeActions.java:10)
    Suppressed: java.lang.RuntimeException: line 4 failed
        at SafeActions.line4(SafeActions.java:41)
        at SafeActions.lambda$main$0(SafeActions.java:7)
        at SafeActions.main(SafeActions.java:7)
Test 15
letting line 1 fail
letting line 2 fail
letting line 3 fail
letting line 4 fail
got exception java.lang.RuntimeException: line 1 failed
    at SafeActions.line1(SafeActions.java:23)
    at SafeActions.main(SafeActions.java:10)
    Suppressed: java.lang.RuntimeException: line 2 failed
        at SafeActions.line2(SafeActions.java:29)
        at SafeActions.lambda$main$2(SafeActions.java:9)
        at SafeActions.main(SafeActions.java:7)
    Suppressed: java.lang.RuntimeException: line 3 failed
        at SafeActions.line3(SafeActions.java:35)
        at SafeActions.lambda$main$1(SafeActions.java:8)
        at SafeActions.main(SafeActions.java:7)
    Suppressed: java.lang.RuntimeException: line 4 failed
        at SafeActions.line4(SafeActions.java:41)
        at SafeActions.lambda$main$0(SafeActions.java:7)
        at SafeActions.main(SafeActions.java:7)

A disadvantage of using the AutoCloseable interface directly, is that it declares to potentially throw Exception and hence, forces us to catch Exception. If the actions don’t throw checked exception or a very specific type, it’s useful to create your own functional interface extending AutoCloseable (In case of IOException, there is already java.io.Closeable).

interface MyAction extends AutoCloseable {
    @Override public void close();
}    
public static void main(String[] args) {
    int currentTest = 11;

    try(MyAction c1 = () -> line4(currentTest);
        MyAction c2 = () -> line3(currentTest);
        MyAction c3 = () -> line2(currentTest)) {
        line1(currentTest);
    }
}

Since this example doesn’t catch exceptions, I also removed the loop which wouldn’t get executed past the second iteration anyway.

letting line 1 fail
letting line 2 fail
line3 executed
letting line 4 fail
Exception in thread "main" java.lang.RuntimeException: line 1 failed
    at SafeActions.line1(SafeActions.java:17)
    at SafeActions.main(SafeActions.java:11)
    Suppressed: java.lang.RuntimeException: line 2 failed
        at SafeActions.line2(SafeActions.java:23)
        at SafeActions.lambda$main$2(SafeActions.java:10)
        at SafeActions.main(SafeActions.java:8)
    Suppressed: java.lang.RuntimeException: line 4 failed
        at SafeActions.line4(SafeActions.java:35)
        at SafeActions.lambda$main$0(SafeActions.java:8)
        at SafeActions.main(SafeActions.java:8)

Upvotes: 1

Always Learning
Always Learning

Reputation: 5581

You can have try/catch blocks within the outer try/catch block if it makes sense to. Something like this:

try{
  line1; // can throw exception
  try {
    line2; // can throw exception
  } catch (Exception e) {
    if (e is a really bad one) throw e;  // don't continue
    else System.out.println("line2 failed but I'll keep going");
  }
  try {
    line3; // can throw exception
  } catch (Exception e) {
    if (e is a really bad one) throw e;  // don't continue
    else System.out.println("line3 failed but I'll keep going");
  }
  line4; // can throw exception
}catch(Exception e){
   handle exception;
}finally{
  do remaining stuffs;
}

Upvotes: 1

Related Questions