Results 1 to 10 of 10

Thread: Purpose of FINALLY Block

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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!


  2. #2
    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


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