Java finally keyword

Source: Internet
Author: User

About the finally statement block, has the following characteristics:

The 1.finally statement block can be directly associated with a try statement block. Try...finally ...

2.try...catch...finally can also

3. The code that is usually in the finally statement block is bound to execute.

See the following example:

import java.io.*;
public class ExceptionTest08{
 public static void main(String[] args){
   try{
     System.out.println("ABC");
     return;
   }finally{
     System.out.println("Test");
   }
 }
}

After compiling the run-time output:

ABC
Test

Typically, return is executed, the method ends, that is, the code after the return is no longer executed. However, since it is followed by a finally statement block, it is said that the finally statement block must be executed, so System.out.println ("Test");

Take a look at the following example:

Import java.io.*;
Public class  exceptiontest08{
  Public  static  Void  main ( string[) args) throws filenotfoundexception{
    try{
     fileinputstream fis= new  FileInputStream ( "C:/ab.txt");   //exception occurred
      //due to an exception in the above code, Therefore System.out.println ("BASIC") will not execute.
     system. out.println ( "BASIC");
   } finally{
      //The code in the finally statement block is bound to execute.
     system. out.println ( "AAA");
   }
 }
}

After compiling the run-time output:

Aaa
ExceptionInchThread"MainJava. io. FileNotFoundException:C +Ab. txt(The system cannot find the file specified.) )
AtJava. io. FileInputStream. Open0 (NativeMethod)
at  java . io . FileInputStream . Open ( unknown  Source)
        at&NBSP; java . io . fileinputstream.< init> ( unknown&NBSP; Source)
        at&NBSP; java . io . fileinputstream.< init> ( unknown&NBSP; Source)
        at&NBSP; ExceptionTest08 . Main ( ExceptionTest08 . Java : $)

There is only one case where the finally statement block is not executed.

import java.io.*;
public class ExceptionTest08{
 public static void main(String[] args){
   try{
     //退出Java虚拟机
     System.exit(0);
   }finally{
     System.out.println("AAA");
   }
 }
}

The above code exits the Java virtual machine in the Try statement block, causing the content in the finally statement block to no longer execute. That is, the contents of the finally statement block are no longer executed as long as the JVM is exited before the finally statement block is executed.

Let's take a closer look at the finally statement block. Take a look at the following example: (Before compiling and running, you can analyze the output of what the result is)

PublicClassexceptiontest09{
PublicStatic&NBSP; void&NBSP; Main ( string[] args) {
    int i=m1 ();
   system. out.println (i);  
 }
  public  static&NBSP; int  M1 () {
    int i= ten;
    try{
      return i;
   } finally{
     i++;
     system. Out.println ( "M1 i=" +i);  
   }
 }
}

After compiling the run-time output:

m1的i=11
10

Take a look at how the above code works:

PublicClassexceptiontest09{
PublicStaticvoidMain string[] args) {
    int i=m1 ();
   system. out.println (i);   //10
 
  Public &NBSP; static&NBSP; Int&NBSP; M1 () {
    int  i= ten;
    try{
      int temp=i;
      return&NBSP;TEMP;
   } finally{
     i++;
     system. Out.println ( "M1 i=" +i);   //11
   }
 }
}

Call the M1 () method in the main method, first execute the SYSTEM.OUT.PRINTLN ("M1 i=" +i) in the M1 () method, output M1 i=11, and then execute System.out.println (i) in the main method; Output 10.

Finally statement block also has a role, finally statement block is bound to execute, so usually in the program, in order to ensure that a resource must be released, generally in the finally statement block to release resources.

Importjava.io.*;
PublicClassexceptiontest10{
PublicStatic void&NBSP; main (string[] args) {
    FileInputStream fis= null;
  &NBSP try{
     fis= new  FileInputStream ( "Exceptiontest10.java");
   } catch (FileNotFoundException e) {
     e.printstacktrace ();
   } finally{
      //In order to ensure that resources are bound to be freed, the code block that frees the resource is placed in the finally statement block.
      if (fis!= null) {
        try{
      & nbsp  fis.close ();
       } catch (IOException e) {
         e.printstacktrace ();
       }
     }
   }
 ,}
}
Search public number "Programmer Koala", please pay attention!

Java finally keyword

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.