JAVA的异常(四):finally关键字

finally代码块:定义一定执行的代码

通常用于关闭资源

finally只有一种情况不执行,就是执行了System.exit(0)的时候,是系统退出,JVM结束

 1 class NegativaException extends Exception
 2 {
 3     NegativaException(String msg)
 4     {
 5         super(msg);
 6     }
 7 }
 8 class Demo
 9 {
10     int div(int a,int b)throws NegativaException
11     {
12         if(b < 0)
13         {
14             throw new NegativaException("出现负数");
15         }
16         return a/b;
17     }
18 }
19 class ExceptionDemo
20 {
21     public static void main(String[] args)
22     {
23         Demo d = new Demo();
24 
25         try
26         {
27             int x = d.div(4,-1);
28             System.out.println("x="+x);
29         }
30         catch(NegativaException e)
31         {
32             System.out.println(e.toString());
33             return;
34         }
35         finally
36         {
37             System.out.println("finally");//finally中存放的是一定会被执行的代码
38         }
39 
40         System.out.println("Over");
41     }
42 }
View Code

 

posted on 2017-06-14 16:14 零EVA 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/hepengke/p/7008965.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值