aspose 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
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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
Greenhorn

Joined: Sep 06, 2012
Posts: 20
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: 32620
    
    4
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: 3026
    
    4

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: 16683
    
  19

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
Greenhorn

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

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?
ven jovovich
Greenhorn

Joined: Sep 06, 2012
Posts: 20
Thanks Winston.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Throwing two exceptions from a method
 
Similar Threads
not able to understand output of the program related to exception handling?
Interesting observation about exceptions
try/catch with exception casting
How to throw same exception from catch block
Concept on coding for Exception Handling