码迷,mamicode.com
首页 > 其他好文 > 详细

多层的异常捕获

时间:2015-11-14 14:59:04      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:

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

 

实际运行结果截图:

技术分享

多层的异常捕获

标签:

原文地址:http://www.cnblogs.com/1995-qxl/p/4964266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!