Java finally块,在之前的教程中,我介绍了try-catch块。在本指南中,我们将看到finally try与try-catch一起使用。
finally块包含所有必须执行的关键语句,无论是否发生异常。无论 try 块是否发生异常,例如关闭连接,流等,此块中的语句将始终执行。
finally块的语法
try {
//Statements that may cause an exception
}
catch {
//Handling exception
}
finally {
//Statements to be executed
}
finally块的简单示例
在这里你可以看到异常发生在try块中,它已经在catch块中被处理,在finally块被执行之后。
class Example
{
public static void main(String args[]) {
try{
int num=121/0;
System.out.println(num);
}
catch(ArithmeticException e){
System.out.println("Number should not be divided by zero");
}
/* Finally block will always execute
* even if there is no exception in try block
*/
finally{
System.out.println("This is finally block");
}
System.out.println("Out of try-catch-finally");
}
}
输出:
Number should not be divided by zero
This is finally block
Out of try-catch-finally
finally块的几个重点
finally块必须与try块相关联,如果没有try块,则不能使用finally块。您应该将这些语句放在必须始终执行的块中。-
finally块是可选的,正如我们在前面的教程中看到的那样,try-catch块足以用于异常处理,但是如果你放置一个finally块,那么它总是在执行try块后运行。 -
在正常情况下,当
try块中没有异常时,则在try块之后执行finally块。但是,如果发生异常,则在finally块之前执行catch块。 -
finally块中的异常行为与任何其他异常完全相同。 -
即使
try块包含诸如return,break或continue之类的控制转移语句,finally块中的语句最终也会执行。
让我们看一个例子,看看当try块中存在return语句时最终是如何工作的:
finally块和return语句的另一个例子
你可以看到,即使我们在方法中有return语句,finally块仍然会运行。
class JavaFinally
{
public static void main(String args[])
{
System.out.println(JavaFinally.myMethod());
}
public static int myMethod()
{
try {
return 112;
}
finally {
System.out.println("This is Finally block");
System.out.println("Finally block ran even after return statement");
}
}
}
以上程序的输出:
This is Finally block
Finally block ran even after return statement
112
finally块未执行时的情况
阻止在finally块中执行代码的情况是:
- 线程的死亡
- 使用
System.exit()方法。 - 由于
finally块中出现异常。
finally和close()
close()语句用于关闭程序中的所有打开流。在finally块中使用close()是一个很好的做法。由于即使发生异常,最终块也会执行,因此无论是否发生异常,您都可以确保所有输入和输出流都已正确关闭。
例如:
....
try{
OutputStream osf = new FileOutputStream( "filename" );
OutputStream osb = new BufferedOutputStream(opf);
ObjectOutput op = new ObjectOutputStream(osb);
try{
output.writeObject(writableObject);
}
finally{
op.close();
}
}
catch(IOException e1){
System.out.println(e1);
}
...
没有catch的finally块
可以在没有catch块的情况下使用try-finally块。这意味着try块可以在没有catch块的情况下使用。
...
InputStream input = null;
try {
input = new FileInputStream("inputfile.txt");
}
finally {
if (input != null) {
try {
in.close();
}catch (IOException exp) {
System.out.println(exp);
}
}
}
...
finally块和System.exit()
System.exit()语句的行为与return语句不同。与return语句不同,每当在try块中调用System.exit()时,finally块不会执行。这是一个代码片段,演示了相同的代码:
....
try {
//try block
System.out.println("Inside try block");
System.exit(0)
}
catch (Exception exp) {
System.out.println(exp);
}
finally {
System.out.println("Java finally block");
}
....
在上面的例子中,如果System.exit(0)被调用而没有任何异常,那么最终将不会执行。但是,如果在调用System.exit(0)时发生任何异常,则将执行finally块。
try-catch-finally块
try语句应该与catch块或finally相关联。- 由于
catch执行异常处理并最终执行清理,因此最好的方法是同时使用它们。
语法:
try {
//statements that may cause an exception
}
catch (…) {
//error handling code
}
finally {
//statements to be executed
}
try-catch-finally块的例子
示例 1:以下示例演示了try块中没有异常时finally块的工作情况
class Example1{
public static void main(String args[]){
try{
System.out.println("First statement of try block");
int num=45/3;
System.out.println(num);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("ArrayIndexOutOfBoundsException");
}
finally{
System.out.println("finally block");
}
System.out.println("Out of try-catch-finally block");
}
}
输出:
First statement of try block
15
finally block
Out of try-catch-finally block
示例 2:此示例显示了在try块中发生异常但在catch块中未处理时finally块的工作:
class Example2{
public static void main(String args[]){
try{
System.out.println("First statement of try block");
int num=45/0;
System.out.println(num);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("ArrayIndexOutOfBoundsException");
}
finally{
System.out.println("finally block");
}
System.out.println("Out of try-catch-finally block");
}
}
输出:
First statement of try block
finally block
Exception in thread "main" java.lang.ArithmeticException: / by zero
at beginnersbook.com.Example2.main(Details.java:6)
正如您所看到的那样,系统生成了异常消息,但在此之前,finally块已成功执行。
例 3 :当try块发生异常并在catch块中正确处理时
class Example3{
public static void main(String args[]){
try{
System.out.println("First statement of try block");
int num=45/0;
System.out.println(num);
}
catch(ArithmeticException e){
System.out.println("ArithmeticException");
}
finally{
System.out.println("finally block");
}
System.out.println("Out of try-catch-finally block");
}
}
输出:
First statement of try block
ArithmeticException
finally block
Out of try-catch-finally block
极客教程