黑马程序员--异常finally

<a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IO开发S</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流!

finally代码块:定义一定执行的代码。
 通常用于关闭资源。把资源释放掉,否则对程序压力非常大。
 
public void method(){

 连接数据库;
 
 数据操作;//throw new SQLException();
 
 关闭数据库;//该动作,无论数据操作是否成功,一定要关闭资源。

}
 try{
  连接数据库;
  数据操作;
} catch(SQLException e){
  数据库异常处理方式;

} finally{
  关闭数据库;

}
------------------------------------------
只有一种情况,finally不会再执行。

package biji.yichang;

public class Demo1 {
 int a(int x,int y) throws FuShuException
 {
  if(y<0)
   throw new FuShuException("出现了除数是负数的情况");
 return x/y;
 }
 static class Demo2{
  public static void main(String[] args)
  {
   Demo1 d=new Demo1();
   
    int b;
    try {
     b = d.a(2,-8);
     System.out.println("b="+b);
    } catch (FuShuException e) {
     
     e.printStackTrace();
     
     System.out.println("除数不要负数");
     //return;//返回了,over不再执行但是finally会执行。
     System.exit(0);//有这句存在,表示程序执行到这里的时候系统推出,jvm结束。finally不会再执行了
    }
    finally{
     System.out.println("finally");
    }
    System.out.println("Over");
      
   }
  }
 public class FuShuException extends Exception{
  FuShuException(String ms){
   super(ms);
  }

 }

 }


 

 <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IO开发S</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值