finally block is optional??

Discussion in 'Java' started by Matt, Jul 1, 2004.

  1. Matt

    Matt Guest

    I want to know if finally block is optional? I tested this myself, and
    the following yield the same output.

    void f()
    {
    try
    { //code1
    }
    catch()
    {
    }
    finally
    { //code2
    }
    }

    void f()
    {
    try
    { //code1
    }
    catch()
    {
    }
    //code2
    }


    please advise. thanks!!
    Matt, Jul 1, 2004
    #1
    1. Advertising

  2. Matt

    Tony Morris Guest

    "Matt" <> wrote in message
    news:...
    > I want to know if finally block is optional? I tested this myself, and
    > the following yield the same output.
    >
    > void f()
    > {
    > try
    > { //code1
    > }
    > catch()
    > {
    > }
    > finally
    > { //code2
    > }
    > }
    >
    > void f()
    > {
    > try
    > { //code1
    > }
    > catch()
    > {
    > }
    > //code2
    > }
    >
    >
    > please advise. thanks!!


    Yes it's optional.
    In your example, you aren't guaranteed the same result each time.
    Hence, there is a purpose to the use of a finally block.

    http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

    Good luck

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
    Tony Morris, Jul 1, 2004
    #2
    1. Advertising

  3. Matt

    Liz Guest

    it is optional

    "Matt" <> wrote in message
    news:...
    > I want to know if finally block is optional? I tested this myself, and
    > the following yield the same output.
    >
    > void f()
    > {
    > try
    > { //code1
    > }
    > catch()
    > {
    > }
    > finally
    > { //code2
    > }
    > }
    >
    > void f()
    > {
    > try
    > { //code1
    > }
    > catch()
    > {
    > }
    > //code2
    > }
    >
    >
    > please advise. thanks!!
    Liz, Jul 1, 2004
    #3
  4. Matt

    Liz Guest

    it is optional

    "Matt" <> wrote in message
    news:...
    > I want to know if finally block is optional? I tested this myself, and
    > the following yield the same output.
    >
    > void f()
    > {
    > try
    > { //code1
    > }
    > catch()
    > {
    > }
    > finally
    > { //code2
    > }
    > }
    >
    > void f()
    > {
    > try
    > { //code1
    > }
    > catch()
    > {
    > }
    > //code2
    > }
    >
    >
    > please advise. thanks!!
    Liz, Jul 1, 2004
    #4
  5. Matt wrote:
    > I want to know if finally block is optional? I tested this myself, and
    > the following yield the same output.
    >
    > [snip]
    > please advise. thanks!!


    Hi,

    yes, it's optional. You only need it, if you want code to be executed in
    any case, even if an exception is thrown. You can even write a
    try/finally without catch, for example:

    public static void finallyTest() throws Exception
    {
    try {
    throw new Exception("my very own Exception");
    }
    finally {
    System.out.println("This is printed anyway");
    }

    }


    Stefan


    P.S.: You'd rather post your newbie questions in comp.lang.java.help, or
    better, get a book where basics like this are explained, for example
    "Thinking in Java" by Bruce Eckel - free to download from one of the
    mirrors at:

    <http://mindview.net/Books/TIJ/DownloadSites>

    Thanks!
    Stefan Waldmann, Jul 1, 2004
    #5
  6. Matt wrote:

    > I want to know if finally block is optional? I tested this myself, and
    > the following yield the same output.


    Not always.
    E.g. 1. If code1 throws an exception not catched by your catch statement
    then code2 is not executed in your second example.
    E.g. 2. If your catch statement throw an exception
    then code2 is not executed in your second example.
    E.g. 3. etc...

    - Dario
    =?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRo, Jul 1, 2004
    #6
  7. Matt

    Liz Guest

    it is optional

    "Matt" <> wrote in message
    news:...
    > I want to know if finally block is optional? I tested this myself, and
    > the following yield the same output.
    >
    > void f()
    > {
    > try
    > { //code1
    > }
    > catch()
    > {
    > }
    > finally
    > { //code2
    > }
    > }
    >
    > void f()
    > {
    > try
    > { //code1
    > }
    > catch()
    > {
    > }
    > //code2
    > }
    >
    >
    > please advise. thanks!!
    Liz, Jul 1, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    4
    Views:
    709
    Adam Maass
    Jul 1, 2004
  2. dannyDog
    Replies:
    56
    Views:
    47,454
    George Cherry
    Jun 15, 2005
  3. Steve Claflin
    Replies:
    2
    Views:
    377
    Rhino
    Feb 13, 2006
  4. morrell
    Replies:
    1
    Views:
    925
    roy axenov
    Oct 10, 2006
  5. David Lozzi

    Try...Catch...Finally not firing finally?

    David Lozzi, Apr 23, 2007, in forum: ASP .Net
    Replies:
    12
    Views:
    777
    Alvin Bruney [MVP]
    May 11, 2007
Loading...

Share This Page