This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes throwing exception from finally block (try-finally clause without catch block) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "throwing exception from finally block (try-finally clause without catch block)" Watch "throwing exception from finally block (try-finally clause without catch block)" New topic
Author

throwing exception from finally block (try-finally clause without catch block)

Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
Hi all.

Here is the code example:



And my question is: what's happening with "e" exception? It seems that jvm completely forgot about it.
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19728
    
  10

Yes, that's one way of describing it. As you found out, the exception thrown in the finally block replaces the exception thrown in the try block. There can only be one exception thrown by a method, of course, so it's necessary to choose one. The choice is: the last one thrown.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24190
    
  34

As a result, most people consider it very bad style to throw an exception -- or allow one to be thrown -- from a finally block.


[Jess in Action][AskingGoodQuestions]
Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
Many thanks all. It's very usefull information.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: throwing exception from finally block (try-finally clause without catch block)
 
It's not a secret anymore!