📜  为什么需要关闭 finally 块中的Java流?

📅  最后修改于: 2022-05-13 01:54:25.951000             🧑  作者: Mango

为什么需要关闭 finally 块中的Java流?

在Java中finally 块是用于执行重要和通用代码的块。 finally 块主要在异常处理期间使用 try 和 catch 来关闭流和文件。 finally 块中的代码不管是否有异常都会执行。这确保所有打开的文件都正确关闭,所有正在运行的线程都正确终止。因此,文件中的数据不会被损坏,并且用户处于安全状态。

finally 块在 try 和 catch 块之后执行。代码的流程是:

  • 尝试
  • 抓住
  • 最后

下面提到了何时引发异常和何时不引发异常的两个示例。

示例 1代码中的异常

Java
// Java program to show the execution of the code
// when exception is caused
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        try { 
            
            // open files
            System.out.println("Open files");
            
            // do some processing
            int a = 45;
            int b = 0;
            
            // divinding by 0 to get an exception
            int div = a / b;
            
            System.out.println("After dividing a and b ans is " + div);
        }
  
        catch (ArithmeticException ae) {
            System.out.println("exception caught");
  
            // display exception details
            System.out.println(ae);
        }
  
        finally {
            System.out.println("Inside finally block");
  
            // close the files irrespective of any exception
            System.out.println("Close files");
        }
    }
}


Java
// Java program to show the execution of the code
// when exception is not caused
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        try { 
            
            // open files
            System.out.println("Open files");
            
            // do some processing
            int a = 45;
            int b = 5;
            
            int div = a / b;
            System.out.println("After dividing a and b ans is " + div);
        }
  
        catch (ArithmeticException ae) {
            
            System.out.println("exception caught");
  
            // display exception details
            System.out.println(ae);
        }
  
        finally {
            
            System.out.println("Inside finally block");
  
            // close the files irrespective of any exception
            System.out.println("Close files");
        }
    }
}


输出
Open files
exception caught
java.lang.ArithmeticException: / by zero
Inside finally block
Close files

例2无一例外

Java

// Java program to show the execution of the code
// when exception is not caused
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        try { 
            
            // open files
            System.out.println("Open files");
            
            // do some processing
            int a = 45;
            int b = 5;
            
            int div = a / b;
            System.out.println("After dividing a and b ans is " + div);
        }
  
        catch (ArithmeticException ae) {
            
            System.out.println("exception caught");
  
            // display exception details
            System.out.println(ae);
        }
  
        finally {
            
            System.out.println("Inside finally block");
  
            // close the files irrespective of any exception
            System.out.println("Close files");
        }
    }
}
输出
Open files
After dividing a and b ans is 9
Inside finally block
Close files