/*
异常随堂练习,分享代码,为刚加入黑马的你!
*/
class Finally
{
public static void main(String[] args)
{
int x;
Demo d =new Demo();
try
{
x=d.div(4,-1);
System.out.println("x="+x);
}
catch (FuShuException e)
{
System.out.println(e);
//System.out.println(e.toString());
}
finally
{
System.out.println("finally....");
}
System.out.println("over");
}
}
class FuShuException extends RuntimeException
{
FuShuException(String s)
{
super(s);
}
}
class Demo
{
int div(int a ,int b)// throws FuShuException
{
if(b<0)
throw new FuShuException("除以负数错误");
return a/b;
}
}
|
|