package duixiang;
class ZiException extends Exception
{
private int num;
ZiException(String s)
{
super(s);
}
ZiException(String s,int num)
{
super(s);
this.num=num;
}
public int getNum()
{
return num;
}
}
class Demo
{
static int div(int a,int b)throws ZiException//函数上抛出异常,可抛多个
{
if(b<0)
throw new ZiException("fushu",7);//函数体中抛出异常
if(b==0)
throw new ArithmeticException("chuling");//Runtime 的子类,不需要向调用函数抛出异常
return a/b;
}
}
public class YiChanDemo {
public static void main(String [] args)
{
try{
int d= Demo.div(1, -1);
System.out.println(d);
System.out.println("over");
}catch(ZiException e){
System.out.println(e.toString());
System.out.println(e.getNum());
}
catch(ArithmeticException e)
{
System.out.println(e.toString());
}
finally{
System.out.println("Thanks for using!!!");//一定执行的方法区
}
}
}