Java 实例 - 链试异常
以下实例演示了使用多个 catch 来处理链试异常:
Main.java 文件
publicclassMain{publicstaticvoidmain(Stringargs[])throwsException{intn=20,result=0;
try{result=n/0;
System.out.println("结果为"+result);
}catch(ArithmeticExceptionex){System.out.println("发算术异常: "+ex);
try{thrownewNumberFormatException();
}catch(NumberFormatExceptionex1){System.out.println("手动抛出链试异常 : "+ex1);
}}}}
以上代码运行输出结果为:
发算术异常: java.lang.ArithmeticException: / by zero 手动抛出链试异常 : java.lang.NumberFormatException