开发者社区 问答 正文

使用多个 catch 来处理链试异常

使用多个 catch 来处理链试异常

展开
收起
珍宝珠 2020-02-13 17:33:25 1613 分享 版权
1 条回答
写回答
取消 提交回答
  • 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
    
    2020-02-13 17:33:58
    赞同 展开评论
问答地址: