The application of finally clause in Java exception processing _java

Source: Internet
Author: User
Tags exception handling finally block

When an exception is thrown, the execution of the method usually makes a steep non-linear turn. Depending on how the method is encoded, an exception can even cause the method to return prematurely. This is a problem in some ways. For example, if a method opens a file entry and closes it and then exits, you do not want the code to close the file to be bypassed by the exception handling mechanism. Finally the keyword is designed to handle this type of accident.

Finally, create a code block. The code block executes before another try/catch occurs after a try/catch block completes. Finally blocks are executed regardless of whether any exception is thrown. If the exception is thrown, finally it is executed even in the absence of a catch clause that matches the exception. A method will return from a try/catch block to the calling program at any time, after an unhandled exception or an explicit return statement, and the finally clause will still execute before the method returns. This is useful for closing the file handle and releasing any other resources that were assigned at the start of the method. Finally clauses are optional and can be either or none. However, each try statement requires at least one catch or finally clause.

The following example shows 3 different exit methods. Each has a finally clause executed:

//demonstrate finally. Class Finallydemo {//through an.
      static void Proca () {try {System.out.println ("inside Proca");
    throw New RuntimeException ("demo");
    finally {System.out.println ("Proca ' s finally");
  }//Return to within a try block.
      static void Procb () {try {System.out.println ("inside PROCB");
    Return
    finally {System.out.println ("PROCB ' s finally");
  }//Execute a try block normally.
    static void Procc () {try {System.out.println ("inside PROCC");
    finally {System.out.println ("Procc ' s finally");
    } public static void Main (String args[]) {try {proca ();
    catch (Exception e) {System.out.println ("Exception caught");
    } PROCB ();
  PROCC (); }
}

In this case, Proca () prematurely interrupts the try by throwing an exception. The finally clause executes when exiting. The try statement of the PROCB () exits with a return statement. The finally clause executes before PROCB () returns. In Procc (), the Try statement executes correctly with no errors. However, finally blocks will still be executed.

Note: If the finally block is used in conjunction with a try, the finally block is executed before the try ends.

The following is the output produced by the above program:

Inside Proca
Proca ' s finally
Exception caught inside PROCB procb
' s finally
inside
PROCC PROCC ' s finally

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.