以下实例演示了使用多个 catch 来处理链试异常:
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

相关文章:

  • 2021-10-08
  • 2021-05-20
  • 2022-12-23
  • 2022-01-06
  • 2021-08-04
  • 2022-01-10
  • 2022-12-23
  • 2021-11-20
猜你喜欢
  • 2022-02-10
  • 2021-07-16
  • 2021-07-09
  • 2022-12-23
  • 2021-08-12
  • 2022-12-23
相关资源
相似解决方案