java finally块_Java finally块

try块也可以有零个或一个finally块。 finally块总是与try块一起使用。

语法

finally块的语法是:

finally {

// Code for finally block

}

finally块以关键字finally开始,后面紧跟一个大括号和一个大括号。finally块的代码放在大括号内。

try,catch和finally块有两种可能的组合:try-catch-finally或try-finally。try块可以后跟零个或多个catch块。try块最多可以有一个finally块。try块必须有一个catch块和一个finally块或两者都有。try-catch-finally块的语法是:

try {

// Code for try block

}

catch(Exception1 e1) {

// Code for catch block

}

finally {

// Code for finally block

}

try-finally块的语法是:

try {

// Code for try block

}

finally {

// Code for finally block

}

无论在相关联的try块和/或catch块中发生什么,finally块中的代码都保证执行。所以,一般使用finally块来写清理代码。

例如,可能用来释放一些资源,如数据库连接的关闭,文件的关闭等,当完成它们时,必须释放。

在try-finally块允许实现这个逻辑。代码结构将如下所示:

try {

// Obtain and use some resources here

}

finally {

// Release the resources that were obtained in the try block

}

示例-1

下面的代码演示了finally块的使用。

public class Main {

public static void main(String[] args) {

int x = 10, y = 0, z = 0;

try {

System.out.println("Before dividing x by y.");

z = x / y;

System.out.println("After dividing x by y.");

} catch (ArithmeticException e) {

System.out.println("Inside catch block a.");

} finally {

System.out.println("Inside finally block a.");

}

try {

System.out.println("Before setting z to 2.");

z = 2;

System.out.println("After setting z to 2.");

}

catch (Exception e) {

System.out.println("Inside catch block b.");

} finally {

System.out.println("Inside finally block b.");

}

try {

System.out.println("Inside try block c.");

}

finally {

System.out.println("Inside finally block c.");

}

try {

System.out.println("Before executing System.exit().");

System.exit(0);

System.out.println("After executing System.exit().");

} finally {

// This finally block will not be executed

// because application exits in try block

System.out.println("Inside finally block d.");

}

}

}

上面的代码生成以下结果。

Before dividing x by y.

Inside catch block a.

Inside finally block a.

Before setting z to 2.

After setting z to 2.

Inside finally block b.

Inside try block c.

Inside finally block c.

Before executing System.exit().

重新抛出一个异常

捕获的异常可以被重新抛出。

public class Main {

public static void main(String[] args) {

try {

m1();

} catch (MyException e) {

// Print the stack trace

e.printStackTrace();

}

}

public static void m1() throws MyException {

try {

m2();

} catch (MyException e) {

e.fillInStackTrace();

throw e;

}

}

public static void m2() throws MyException {

throw new MyException("An error has occurred.");

}

}

class MyException extends Exception {

public MyException() {

super();

}

public MyException(String message) {

super(message);

}

public MyException(String message, Throwable cause) {

super(message, cause);

}

public MyException(Throwable cause) {

super(cause);

}

}

上面的代码生成以下结果。

MyException: An error has occurred.

at Main.m1(Main.java:14)

at Main.main(Main.java:4)

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值