File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Throwing two exceptions from a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Throwing two exceptions from a method" Watch "Throwing two exceptions from a method" New topic
Author

Throwing two exceptions from a method

ven jovovich
Ranch Hand

Joined: Sep 06, 2012
Posts: 33
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

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ven jovovich
Ranch Hand

Joined: Sep 06, 2012
Posts: 33
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

Joined: Mar 17, 2011
Posts: 8008
    
  22

ven jovovich wrote:Is it just GCed?

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

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
ven jovovich
Ranch Hand

Joined: Sep 06, 2012
Posts: 33
Thanks Winston.
 
Don't get me started about those stupid light bulbs.
 
subject: Throwing two exceptions from a method