Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

finally block consumes/stifles exception

Thread view: 
Steve Claflin - 13 Feb 2006 12:14 GMT
I was rather surprised at the outcome of this code, which I created to show
someone how a finally block will run even if the try or catch block returns
from the method:
Loveen - 13 Feb 2006 13:50 GMT
Hi
Take this example...

public class DebugFinally {
    public static void main(String[] args) {
        try{
            int number = Integer.parseInt("1245");
        }catch(Exception ex){
            System.out.println("In Catch");
            return;
        }finally{
            System.out.println("In Finally");
        }
        System.out.println("Not Returned");
    }
}

Output:
In Catch
In Finally

As you see above the program performed what was written in Catch but
after that it directly goes to finally block and execute what ever is
written in it, after that it comes back to the catch block to call the
return.

Basically internally the compiler executes whatever is written in the
finally block before executing any statement which takes the control
out of try/catch construct.

But, there is one exception to this. If I call System.exit(0); in place
of return then, program would end abruptly without giving care to
whatever is written in finally block.

And in that scenario the output would be modified as follows:

}catch(Exception ex){
            System.out.println("In Catch");
            System.exit(0);
        }finally{
            System.out.println("In Finally");
        }

Output:
In Catch
Rhino - 13 Feb 2006 13:52 GMT
>I was rather surprised at the outcome of this code, which I created to show
>someone how a finally block will run even if the try or catch block returns
>from the method:

Maybe you'd like to try again? This time _with_ a code fragment?

--
Rhino


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.