//仅作为学习笔记
/*
finally 代码块;定义一定执行的代码
通常用于关闭资源
public void method ()throws SQLException
{
try
{
连接数据库;
数据操作;
}
catch(SQLException e)
{
会对数据库进行异常处理;
}
finally
{
关闭数据库;
}
}
*/
class FuShuException extends Exception
{
FuShuException(String msg)
{
super(msg);
}
}
class Demo
{
int div(int a,int b) throws FuShuException
{
if(b < 0)
{
throw new FuShuException("出现了除数是负数的情况!");
}
return a/b;
}
}
class ExceptionDemo
{
public static void main(String[] args)
{
Demo d = new Demo();
try
{
int x = d.div(4,-1);
System.out.println(" x = "+ x);
}
catch (FuShuException e)
{
System.out.println(e.toString());
return ;
}
finally
{
System.out.println("finally内的代码是一定会执行的!");
}
System.out.println("It is over!");
}
}
![](https://img-my.csdn.net/uploads/201303/07/1362667546_4641.jpg)
格式一:
try{}
catch{}
格式二:
try{}
catch{}
finally{}
格式三:
try{}
finally{}
记住一点catch 是用来处理异常的,如果没有catch就代表异常没有被处理过,、
如果该异常时检测时异常,就必须进行声明.