permaculture playing cards*
The moose likes Java in General and the fly likes Is finally, really finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is finally, really finally" Watch "Is finally, really finally" New topic

Is finally, really finally

Manoj Pooleery
Ranch Hand

Joined: Sep 20, 2000
Posts: 35
I have the following code block -
public class ExceptionTest {
public static void main(String[] args) {
try {
ExceptionTest test = new ExceptionTest();;
} catch (MyException ex) {
System.out.println("An Error!!");
public void foo()
throws MyException {
int j = 0;
try {
j = 5 / 0; // will give an error since you cannot divide by zero
} catch (Exception e) {
throw new MyException();
} finally {
class MyException
extends Exception {
public MyException() {
System.out.println("MyException() Constructor Invoked");
I am getting an output of
MyException() Constructor Invoked
An Error!!
According to the exception handlign mechanish, if there is a finally block, then, execution continues after the exception is caught into the finally block. Then, how is it showing the message in the exception block of the calling method? Also, is it safe to have clean-up code in the finally block of individual methods?
Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
finally{} will define the last actions in the method foo() when an exception is caught in your try/catch/finally block. It is not the last thing that the application will do.
foo() just returns to main() where you could have coded another finally{} block.
[This message has been edited by Colin Kenworthy (edited October 22, 2001).]
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Execution continues after the finally block if the exception is handled/caught. You did not catch the exception, you re-threw it in your catch block. Hence, you are delegating handling the exception to the calling method.


So much trouble in the world -- Bob Marley
Manoj Pooleery
Ranch Hand

Joined: Sep 20, 2000
Posts: 35
Thanks to the replies. But still, I have these doubts - on the occurrence of an exception, I want the calling method to handle it, at the same time, I want some cleanup to be done. Relating this to the flow of control, when I throw the exception up to the calling method, will the control be transferred to the callee (before or) after the finally block is executed? Also, is it a good design consideration to have the cleanup code in a finally block in the child method?
Second doubt relating to Exceptions - regarding the throwing of the Exceptions, what is the difference between throwing a new exception with Exception.getmessage() and the exception itself? Does it affect the stack trace in any way?
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
The finally clause is exactly the right place to do cleanup in the called method. Just beware that sometimes, the cleanup code can throw exceptions as well; if an exception has already been thrown, you don't want any cleanup exceptions to obscure it. For exampleIn this example, exceptions are caught and re-thrown. You won't find any of this back in the stack trace: it will give the location where the exception was first thrown. If you don't want this, call Throwable.fillInStackTrace() or throw a brand new Exception (bad practice in most cases, unless the new exception handles nested exceptions - see the source for java.rmi.RemoteException for an example of proper nested exception handling).
- Peter
I agree. Here's the link:
subject: Is finally, really finally
Similar Threads
The throw question
Natr... mockQ 19
Exception error
Exceptional Handling Question