Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Throwing two exceptions from a method

 
ven jovovich
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Please refer to the below code. I am not getting what happens to "exception 1". Seems that the JVM eats up the exception.


output:


I expected the JVM to crash since B.methB() can handle only one exception.
Please help.

Regards,
Ven.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50240
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider that a finally specifies something which is happens whether or not an Exception is thrown in its preceding try. What you are saying is something like
Whether or not an Exception occurs in the try, I want an AWTException.
So the AWT Exception hides the original Exception. This is normal. You can’t have two Exceptions simultaneously in the same thread. The compiler can tell that if it allows that code to compile without your declaring the Exception.
If you go through the Java Tutorials section, you find you can chain Exceptions together if you want several Exceptions.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JVM doesn't eat the exception, your code hides it. Only one exception can be thrown from a method. The finally clause will execute regardless of what happens in the try or catch clause. So if the try throws an exception, the finally gets called anyways. Then the finally clause causes an exception. Since the finally clause was called after the try's exception was thrown its exception takes precedence and is thrown to the caller.

You should never do this. Do not let exceptions escaped a finally clause.
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ven jovovich wrote:
I expected the JVM to crash since B.methB() can handle only one exception.
Please help.


And did you really expect the JVM to crash? If it was that easy to crash a JVM, you will wind up with very unstable web and app servers.

Henry
 
ven jovovich
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell, Steve and Henry.
Henry, by "crash" I meant that the JVM would spit out the unhandled exception and exit.
BTW, what happens to the object created on line 10? Is it just GCed?

Regards,
Ven.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ven jovovich wrote:Is it just GCed?

I imagine so. After all, there aren't any outside references to it.

Winston
 
ven jovovich
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic