0

In the following code two exceptions [FatalException and MyException] are thrown. The Trivial Myexception (overwrites?) Makes the FatalException to be lost. how?

how to handle this situation?

    try{
        try {
        throw new FatalException("Important");
        } 
    finally
    {              
          throw new MyException("trivial");
    }
    }catch (Exception ex) {
        ex.printStackTrace();
    }
}

without the catch class for inner try block.

(Article Mentioned its a flaw Exceptions in java!) so there should be someway to overcome it? maybe recent standards changed it?

Dineshkumar
  • 4,165
  • 5
  • 29
  • 43

3 Answers3

2

There is no way to handle this elegantly: finally takes over the control in Java, letting you do whatever you want, even return normally from a try block that threw an exception.

If you must handle the inner exception, you need to write a lot of potentially fragile code for it:

Exception important = null;
try{
    try {
        important = new FatalException("Important");
    } finally {
        if (important != null) throw important;
        throw  new MyException("trivial");
    }
} catch (Exception ex) {
    ex.printStackTrace();
}

Your best solution is not to write code like that.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

This is because the order of execution. The initial throw for all intents and purposes is the defacto return statement for the method (Read: exit point) but because it is inside a try block with a finally block the code inside the finally block is guaranteed to be run before the method returns (with a few exceptions). So before the methods return/throw another, different exception is thrown which is a newer, sooner method return.

The way to handle this situation it to never get in it. You should be aware that this is how Java behaves and avoid programming yourself in this corner.

Community
  • 1
  • 1
Craig
  • 1,390
  • 7
  • 12
  • it is not a guarantee that it will run. What if i pull the power source before the finally gets executed? – Woot4Moo May 28 '13 at 03:29
  • @Woot4Moo if you pull the power source, the method return will also not happen. – Matt Ball May 28 '13 at 03:30
  • right so your statement is incorrect. – Woot4Moo May 28 '13 at 03:31
  • @Woot4Moo: I am technically correct, as I said that it is guaranteed to be run before the **method returns**. I did not say anything about when the method fails to return. – Craig May 28 '13 at 03:34
  • you aren't even technically correct. I can terminate the process at any point and invalidate your statement. – Woot4Moo May 28 '13 at 11:45
0

I guess that this could be the correct structure of how you could nest the exceptions.

try
{
    // Some codes to be tried over here.
}
catch(FatalException fe)
{
    System.out.println("Fatal Exception occurred.");
}
catch(Exception e)
{
    System.out.println("Error! ");
}
catch(MyException me){}

In this manner, the exceptions could be caught accordingly.