class FuShu extends Exception
{
}
public int div(int a ,int b)throws FuShu
{
if(b< 0)
{
throw new FuShu();
}
return a/b;
}
public static void main(String[] args) throws FuShu
{
chat cc = new chat();
try {
System.out.println(cc.div(4, -1));
} catch (Exception e) {
System.out.println("出现负");
}
}
注: try块执行后徐对应的catch块跟着只会执行一个,不关try后面跟着多少个catch但是只会执行一个catch。除非,在循环中使用了continue开始下一个循环又运行try,这样可能导致
运行多个catch。