• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Throwing two exceptions from a method

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston.
reply
    Bookmark Topic Watch Topic
  • New Topic