aspose file tools*
The moose likes Java in General and the fly likes benfit of having try catch finaly instead of try catch as code after catch will run in either case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "benfit of having try catch finaly instead of try catch as code after catch will run in either case" Watch "benfit of having try catch finaly instead of try catch as code after catch will run in either case" New topic
Author

benfit of having try catch finaly instead of try catch as code after catch will run in either case

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 541
if we have try catch finally , then instead of having finally if we write the code which should be in finally block without a finally block after catch, then also it will be executed in either case. So what is the benfit we got by using try catch finally instead of try catch.

thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Monica. Shiralkar wrote:if we have try catch finally , then instead of having finally if we write the code which should be in finally block without a finally block after catch, then also it will be executed in either case. So what is the benfit we got by using try catch finally instead of try catch.


Basically, your premise is incorrect. The finally block will execute, even if exceptions are thrown that are not caught (in cases where some exceptions are caught, but not others), or a return is executed in the try or catch blocks (which normally, mean that the code after the try-catch will not run). The finally block run when the try-catch completes, regardless of how it completes.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ashish bassii
Greenhorn

Joined: Sep 05, 2008
Posts: 12
The code mentioned in finally block will execute even if the exception thrown is not handled by any of the exception blocks, so if you have try block, a catch block for ArithmeticException, and a finally block.. but the exception you actually got in try block is IOExcepton, the finally block will still execute, but the code after catch block wont
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 541
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: benfit of having try catch finaly instead of try catch as code after catch will run in either case
 
Similar Threads
Can we use finaly block to catch exceptions without using catch block?
try/catch/finally
Unreachable statement
Catch and Finally Block.
SCJP 1.4 K&B Book Chp 4 clarification reqd.