wood burning stoves 2.0*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 36478
    
  16
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: 3944
    
  17

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: 18108
    
  39

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: 7032
    
  16

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?
Artlicles by Winston can be found here
ven jovovich
Ranch Hand

Joined: Sep 06, 2012
Posts: 33
Thanks Winston.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Throwing two exceptions from a method
 
Similar Threads
Interesting observation about exceptions
not able to understand output of the program related to exception handling?
How to throw same exception from catch block
try/catch with exception casting
Concept on coding for Exception Handling