This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes code following the finally block. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "code following the finally block." Watch "code following the finally block." New topic
Author

code following the finally block.

keisin syu
Greenhorn

Joined: Dec 04, 2002
Posts: 25
I had met a statement about try, catch and finally block in the pages"code after finally block"(name is add by myself).
here is quote from the page:

If an exception is thrown, then the rest of the code in the try block is not executed. If any catch block matches the exceptions class or a super class of the exception, that block executes. If the exception is not caught correctly, then after the finally block executes, the rest of the code in the method is not executed.

and i don't understand the last sentance and do some test with the code here.
and the code after finally block is executed to print out "after finally".

am i misunderstanding the meaning or is it wrong the statement of the last sentence?
[ December 12, 2002: Message edited by: keisin syu ]
Keen Chen
Ranch Hand

Joined: Nov 12, 2002
Posts: 47
HI, I think because in ur code, the exception is a handled exception. it's caught correctly.


SCJP 1.4 100% @ Peking, China <br />~~~~~~~~~~~~~~~~~~~~~<br />但使龙城飞将在, 不教胡马度阴山!
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
If the exception is not caught correctly, then after the finally block executes, the rest of the code in the method is not executed.

your code catches the exception, so the rest of the method body is executed after the finally block and the method doesn't terminate or populate the exception to the caller.
[ December 12, 2002: Message edited by: Alfred Kemety ]

Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Levente Szekrenyes
Greenhorn

Joined: Mar 08, 2002
Posts: 29
Keen is right, your exception is handled correctly, that's why the text "after finally" prints out. If you modify your code like this:

you will notice that your exception is not properly caught, so when run, the program prints out:
"before exception"
"333"
and then just exits with an error messsage, without printing "after finally".
Note that, because the exception wasn't properly caught, I also had to declare that the main method might throw an Exception...
keisin syu
Greenhorn

Joined: Dec 04, 2002
Posts: 25
thank you all.
it looks like i'm not reading the quote carefully and get lost when i try my code:-)
 
 
subject: code following the finally block.
 
Similar Threads
try..catch flow???
Try catch flow
flow control
exception..try catch block execution
Exceptions Execution flow