|
Exception Handling
|
|
08-13-2008, 06:20 PM
Post: #1
|
|||
|
|||
|
Exception Handling
Hi Everyone...
I am confused with one example of try..catch..finally.. so plz help me out in getting it correctly class Test_Exception { static void test() throws IOException { try { throw new IOException(); } finally { System.out.println("In Finally");///line 1 } } public static void main(String []arg) { try { test(); }catch(Exception e) { System.out.println("In Catch"); } System.out.println("End Main"); } } the output is In Finally In Catch End Main why after finally this catch block executed?? and if line is replaced by catch block like this catch(IOException ioe) { System.out.println("In Catch_test"); } then what o/p is different now?? Thanks in Advance. |
|||
|
08-13-2008, 07:30 PM
Post: #2
|
|||
|
|||
|
RE: Exception Handling
The finally block always executes when the try block exits and finally block is used for any performing any clean up operations regardless of any problem occurred. In your code the catch block got executed because in your test method you are not catching the exception. so the exception will be passed to the caller method for handling. The exception handler will find a catch block that catches the IOException thrown from the test method inside your main method. Try and finally without catch will not handle your exception.
When you change the line 1 to a catch block, the exception will be handled from test method itself. So the control will not go to the catch block inside your main method. |
|||
|
08-13-2008, 07:40 PM
Post: #3
|
|||
|
|||
|
RE: Exception Handling
sorry to argue on this .....
but for my better understanding... i accept it that control will be passes to the catch becaz i have not provided handler in the test method. but my question is first JVM will look for the handler and handler is in the main method... so it should first execute the catch block and then finally.... can u just give some light on the flow of execution?(try.....finally...catch) how come finally is executed before the catch?? is it becaz of the finally method local to the test method?? |
|||
|
08-13-2008, 07:59 PM
Post: #4
|
|||
|
|||
|
RE: Exception Handling
sorry to argue on this .....
but for my better understanding... i accept it that control will be passes to the catch becaz i have not provided handler in the test method. but my question is first JVM will look for the handler and handler is in the main method... so it should first execute the catch block and then finally.... can u just give some light on the flow of execution?(try.....finally...catch) how come finally is executed before the catch?? is it becaz of the finally method local to the test method?? |
|||
|
08-13-2008, 08:08 PM
(This post was last modified: 08-13-2008 08:22 PM by albinjoseph.)
Post: #5
|
|||
|
|||
|
RE: Exception Handling
From Java Language Specification . http://java.sun.com/docs/books/jls/first...html#44153
"When an exception is thrown, control is transferred from the code that caused the exception to the nearest dynamically-enclosing catch clause of a try statement that handles the exception." So whenever an exception occurs Java will look for the immediate catch block. If a catch block is not found, it will look into the caller method. This process repeats till we reach the last caller method and still if JVM couldn't find the handler the default handler would handle the exception. The flow of execution will be 1. test method's catch block if present 2. once all the catch block's completed, finally block 3. if no catch present in test method 4. main method's catch block. 5. main method's finally block |
|||
|
08-13-2008, 08:19 PM
Post: #6
|
|||
|
|||
|
RE: Exception Handling
thanks buddy..
thanks for the information. |
|||
|
« Next Oldest | Next Newest »
|

Search
Member List
Calendar
Help




