Results 1 to 10 of 10

Thread: Purpose of FINALLY Block

  1. #1
    Geek_Guest
    Guest

    Purpose of FINALLY Block

    What is the purpose of finally block (i know Mandatory statements are written in finally block) but outside of the catch block statements are also executed right i write mandatory statements are written out side the catch block. so pls tell me finally block need?

    Question asked by visitor Sudheer


  2. #2
    Contributing Member
    Join Date
    Oct 2007
    Answers
    88

    Smile Re: Purpose of FINALLY Block

    Quote Originally Posted by Geek_Guest View Post
    What is the purpose of finally block (i know Mandatory statements are written in finally block) but outside of the catch block statements are also executed right i write mandatory statements are written out side the catch block. so pls tell me finally block need?

    Question asked by visitor Sudheer

    The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.


  3. #3
    Junior Member
    Join Date
    Dec 2007
    Answers
    3

    Re: Purpose of FINALLY Block

    The Main purpose of Finally Block is to close all the files that are opened in the code or any datebases in the code.Here(in Finally Block)we write code to close all datebases ,files etc.
    The Finally Block will Execute Wheteher there is an Exception or not


  4. #4
    Contributing Member
    Join Date
    Nov 2007
    Answers
    46

    Re: Purpose of FINALLY Block

    finally block
    - it handles uncatched exceptions.
    - this must be followed with the try and catch block.
    - if try block throw an exception then no other catch block handle that exception. now finally block executes that exception.
    example:

    try {
    //it throws a zero by division error

    }
    catch(NullPointerException)
    {
    // it throws NullPointerException instead of Zero by division error
    }
    finally{
    printStackTrace();
    }
    - in the above example no catch statement to handle the exception i.e throw by the try then finally block can handle that exception by using the printstacktrace() method. printstacktrace(), which handles any other exption.
    main advantage is to be finally block must be executed.

    note:dont enter any statement between try,catch and finally block even System.out.print() also. if u use compiler gives an error


  5. #5
    Junior Member
    Join Date
    May 2008
    Answers
    2

    Smile Re: Purpose of FINALLY Block

    Quote Originally Posted by rahulvegi View Post
    finally block
    - it handles uncatched exceptions.
    - this must be followed with the try and catch block.
    - if try block throw an exception then no other catch block handle that exception. now finally block executes that exception.
    example:

    try {
    //it throws a zero by division error

    }
    catch(NullPointerException)
    {
    // it throws NullPointerException instead of Zero by division error
    }
    finally{
    printStackTrace();
    }
    - in the above example no catch statement to handle the exception i.e throw by the try then finally block can handle that exception by using the printstacktrace() method. printstacktrace(), which handles any other exption.
    main advantage is to be finally block must be executed.

    note:dont enter any statement between try,catch and finally block even System.out.print() also. if u use compiler gives an error

    1) please note that the finally block will ALLWAYS be executed, no matter what. not just if an exception was not caught.
    2) what happends if an exception is thrown from within the finally block? - aha: the execution of the finally code will stop and the exception will be thrown out of the current function!


  6. #6
    Junior Member
    Join Date
    Jul 2008
    Answers
    1

    Re: Purpose of FINALLY Block

    the Finally is to ensure tht the code enclosed with in this block is xecuted ever if when our program teriminates due uncatched xception


  7. #7
    Junior Member
    Join Date
    Jul 2008
    Answers
    1

    Re: Purpose of FINALLY Block

    finaly block is executed whether or not an exception accures.it will be executed always .The
    main purpose of this is to execute the code that we have to execute in every condition.
    for example we have to close a database connection whether our command executes succesfully or not
    Try
    {
    myComm.executeNonQuery();
    }
    catch(Ex)
    {
    //do something with exception Ex
    return;
    }
    finaly
    {
    myConn.close();
    }
    the main point to see in above code that whether we have applied a return statement in catch block finaly block will still execute.


  8. #8

    Smile Re: Purpose of FINALLY Block

    Purpose of finally block :

    --> The finally block is self explanatory(finallyyyyyy).

    --> The piece of code which you want to execute after come out of
    statements in try block must and should must be written in finally block.
    i.e independent of reslut of try bolck code whether it is successful or not .

    Ex :

    public class FinallyTest
    {
    FinallyTest()
    {
    String str = "1";
    try
    {
    //try to get array or stringIndexOutOfBoundsException

    System.out.println(str.charAt(2));
    }
    catch(NumberFormatException ne)
    {
    ne.printStackTrace();
    System.out.println("catch called");
    }
    finally
    {
    System.out.println("finally called");
    }

    System.out.println("method end called");
    }

    public static void main(String args[])
    {
    FinallyTest objTest1 = new FinallyTest();
    }
    }
    --> Your questiona about after catch block statments also will be executed
    the why finally ,

    Consider above example,
    In that method StringIndexOutOdBOundsException
    occured which is not caught.

    so statements below catch cannot be executed.
    But statments in finally are executed.

    The statments below catch block are executed only
    if you caught the raised exception.

    But the statements in finally block are executed
    even though you caught or uncaught the raised exception


    --> So now I think You Got IT.


  9. #9

    Thumbs up Re: Purpose of FINALLY Block

    Actually out side the catch block u can write the mandatory codes but when an exception occurred before the mandatory statement then these codes are not
    executed so in a program finally block are execute, whether there is some exception occur or not, so the mandatory statement must execute.


  10. #10
    Junior Member
    Join Date
    Jul 2008
    Answers
    2

    Smile Re: Purpose of FINALLY Block

    Hi,
    Follow the snippest code here
    try
    {
    // do something here
    }
    catch
    {
    // If any errors are there then the control will return from here without //executing next code after this catch block
    }
    to do some operation if any exception is thrown from the try block you need finally
    {
    // do other oprations
    }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact