aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Finally" Watch "Finally" New topic
Author

Finally

Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277

Hi,
Here i have some confusion about sequence of output. is it not supposed to first handle the Exception thrown in catch block then execute finally block??
please explain it to me.


Output:
CaughtMalformedURLException
End finally
Exception at main()
regards
vivek

[This message has been edited by Vivek Shrivastava (edited August 08, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Vivek
The reason why you get the following is as follows.
Once the code a==null is successfull, the MalformedURLException is thrown. This exception is caught in the same test() method which prints CaughtMalformedURLException and again throws the same execption.
But before control is transferred to the main, finally() is executed, so it prints
End finally.
Then this exception is caught in the main() method which prints
Exception at main().
Hope this clarifies
Anand
chetan nain
Ranch Hand

Joined: Jun 21, 2000
Posts: 159
dear vivek,
note that MalformedURLexception is a checked exception.
1. this means that this exception must either be caught using the try-catch construct, or , be declared in the throws clause of the invoking method.
2. in your code, this exception is caught using the first catch construct. it is then RETHROWN.
3. since it is checked exception, it has to be declared in the throws clause of invoking method ie test(). note that this is not done. the test() method only throws one IOException.
3. thus, after execution of finally, method is aborted and hence result
hth, chetan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finally