<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>、期待与您交流!