'Execute the remaining code after handling exception in java? [closed]

I may be silly. But I got interviewed and I was asked question regarding how you will run the remaining code after you will get the exception.

I gave number of approaches :

  1. We can write the code in finally.
  2. We can write the code in catch block. (They do not want to handle with these 2 approaches.)
  3. We can use throw keyword. But I tried practically, It's not working.

I tried to explain them with throw statement too.

I have referred so many posts. But My doubt is still not cleared.

As example,

  public static void main(String[] args)
  {
      a(); // getting exception here...
      b(); // This method should executed after handling exception
  } 

It will be helpful if you can suggest any approach on this. So I can understand it.



Solution 1:[1]

If you caught your exception and handled it you can just run your b() method after try-catch block:

try {
  a();
} catch(Exception e) {
  handleMyError(e);
}
b();

This way the a() method executes, if exception is thrown it is caught and handled in the method handleMyError(Exception e) and then the execution continues to b() regardless whether the exception was thrown or not.

Solution 2:[2]

If you want to execute b(); only if the exception is thrown, you should call it on the catch(){} block. If you want to execute it anyway you can put it either on finally {} or after everything (see edit).

  • If b(); is very retalated to the exception or you want to use variables initialized inside the try{} block, you should write it on finally{} such like this:

    try{
          a();
         } catch (Exception e){
          e.printStackTrace();
          //what to do if exception was thrown
         } finnaly {
          b();
         }
         //you can also call b(); here instead of inside finnaly
    
  • If b(); is used to handle the exception then:

    try{
     a();
    } catch (Exception e){
     e.printStackTrace();
     b();
    }
    

You can also make the method to throw A's exception and handle it in the method which is calling a(); But if you are in the main, you should'nt do that.

EDIT: As requested, an exemple of the correct answer that wich the interviewer was asking for:

    try{
      a();
    } catch (Exception e){
      e.printStackTrace(); //optional
    }

    b();

Solution 3:[3]

To me probably the only concise way to do that without try-catch-finally is by using CompetableFuture features and dispatch execution of the functions each as a separate task. Like this:

@Test
public void tryCatchFinallyWithCompletableFuture() throws Exception
{
    CompletableFuture
        .runAsync(
            () -> a()
        )
        .exceptionally(
            (e) -> { System.out.print(e.getMessage()); return null; }
        )
        .thenRun(
            () -> b()
        )
        .get();
}

static void a() {
    throw new IllegalStateException("a()");
}

static void b() {
    System.out.println("...but we continue after a()");
}

However, I would seriously consider using this approach anywhere but in a playground project. Besides, you must remember that a() and b() are executed in a multi-threaded context which may require synchronization for the shared state if any is present.

Solution 4:[4]

You need to execute method in where or finally clause

try {
  a(); // getting exception here...
} catch(Exception e) {
  b(); // This method should executed after handling exception
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CuriousCurie
Solution 2
Solution 3 yegodm
Solution 4 loljeene