欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

Java实例-链试异常

来源: javaer 分享于  点击 807 次 点评:266

Java实例-链试异常


Java 实例 Java 实例

以下实例演示了使用多个 catch 来处理链试异常:

Main.java 文件

public class Main {
public static void main (String args[])throws Exception {
int n=20,result=0;
try{
result=n/0;
System.out.println("结果为"+result);
}
catch(ArithmeticException ex){
System.out.println("发算术异常: "+ex);
try {
throw new NumberFormatException();
}
catch(NumberFormatException ex1) {
System.out.println("手动抛出链试异常 : "+ex1);
}
}
}
}

以上代码运行输出结果为:

发算术异常: java.lang.ArithmeticException: / by zero
手动抛出链试异常 : java.lang.NumberFormatException

Java 实例 Java 实例

用户点评