多层的异常捕获

CatchWho.Javaspa

源代码:blog

public class CatchWho { it

    public static void main(String[] args) { io

        try { class

            try { 程序

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

             } static

            catch(ArrayIndexOutOfBoundsException e) { img

               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

 

实际运行结果截图:

相关文章
相关标签/搜索