zoukankan      html  css  js  c++  java
  • 多层的异常捕获

    CatchWho.Java

    源代码:

    public class CatchWho { 

        public static void main(String[] args) { 

            try { 

                try { 

                    throw new ArrayIndexOutOfBoundsException(); //要处理的问题

                 } 

                catch(ArrayIndexOutOfBoundsException e) { 

                   System.out.println("ArrayIndexOutOfBoundsException"+  "/内层try-catch"); 

                } 

                throw new ArithmeticException(); 

            } 

            catch(ArithmeticException e) { 

                System.out.println("发生ArithmeticException"); 

            } 

            catch(ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException" + "/外层try-catch"); 

            } 

        } 

    }

    预测程序运行结果:ArrayIndexOutOfBoundsException/外层try-catch

                         发生ArithmeticException

                         ArrayIndexOutOfBoundsException/外层try-catch

    实际运行结果截图:

    源代码:

    public class CatchWho2 { 

        public static void main(String[] args) { 

            try {

                try { 

                    throw new ArrayIndexOutOfBoundsException(); 

                } 

                catch(ArithmeticException e) { 

                    System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 

                }

                throw new ArithmeticException(); 

            } 

            catch(ArithmeticException e) { 

                System.out.println("发生ArithmeticException"); 

            } 

            catch(ArrayIndexOutOfBoundsException e) { 

                System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 

            } 

        } 

    }

    预测程序运行结果:ArrayIndexOutOfBoundsException/内层try-catch

                   发生ArithmeticException

                      ArrayIndexOutOfBoundsException/外层try-catch

    实际运行结果截图:

  • 相关阅读:
    洛谷—— P2234 [HNOI2002]营业额统计
    BZOJ——3555: [Ctsc2014]企鹅QQ
    CodeVs——T 4919 线段树练习4
    python(35)- 异常处理
    August 29th 2016 Week 36th Monday
    August 28th 2016 Week 36th Sunday
    August 27th 2016 Week 35th Saturday
    August 26th 2016 Week 35th Friday
    August 25th 2016 Week 35th Thursday
    August 24th 2016 Week 35th Wednesday
  • 原文地址:https://www.cnblogs.com/1995-qxl/p/4964266.html
Copyright © 2011-2022 走看看