zoukankan      html  css  js  c++  java
  • 课件中动手动脑5

    (1)多层的异常捕获1

    源代码:

    public class catch1 {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            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"); 
        } 
        }
    }

    运行结果:

    我对源代码及运行结果的理解:

    内层的try抛出ArrayIndexOutOfBoundsException异常然后被内层的catch捕捉,并输出结果中的第一行,然后异常因为被捕捉,继续执行外层try中的语句,抛出了ArithmeticException异常,被外层的catch语句捕捉后输出结果中的第二行。

    (1)多层的异常捕获2

    源代码:

    public class catch2 {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            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"); 
        } 
        }
    }

    运行结果:

    我对源代码及运行结果的理解:

    内层的try中抛出了异常ArrayIndexOutOfBoundsException,在内层的catch中没有办法捕捉,就跳出了外层的try,被外层的catch捕捉,此时外层的try语句中还有没有被执行的则直接跳过继续向下执行。

    (1)finally执行机制:

    源代码:

    public class finally1 {
        public static void main(String[] args) {
        int result;        
            try {            
                System.out.println("in Level 1");           
                 try {                
                    System.out.println("in Level 2");
      result=100/0;  //Level 2               
                     try {                   
                         System.out.println("in Level 3");                      
                         //result=100/0;  //Level 3                
                    }                 
                    catch (Exception e) {                    
                        System.out.println("Level 3:" + e.getClass().toString());                
                    }                               
                    finally {                    
                        System.out.println("In Level 3 finally");                
                    }                            
                    // result=100/0;  //Level 2            
                    }            
                catch (Exception e) {               
                     System.out.println("Level 2:" + e.getClass().toString());          
                 }
                 finally {                
                    System.out.println("In Level 2 finally");           
                 }             
                // result = 100 / 0;  //level 1        
            }         
            catch (Exception e) {            
                System.out.println("Level 1:" + e.getClass().toString());        
            }        
            finally {         
             System.out.println("In Level 1 finally");
            }    
        }
    }

    运行结果:

    我对源代码及运行结果的理解:

    在多层的try。。Catch()。。Finally执行时,某一层有错误被抛出后,该错误只能被本层或者外层的catch所捕捉,也就意味着该错误一旦出现,在其之后的所有语句不会被执行,直到其被本层或者更外层的catch所捕捉,该catch之后的语句才能被执行,也就是只有该catch所在的那一层以及更外层的finally语句才能被执行。

    4finally语句块一定会执行吗?

    public class finally2 {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    try{            
                System.out.println("in main");           
                throw new Exception("Exception is thrown in main");
                        //System.exit(0);        
            }       
            catch(Exception e)
                {            
                System.out.println(e.getMessage());            
                System.exit(0);        
            }       
            finally        
            {            
                System.out.println("in finally");        
            }
        }
    }

    运行结果:

    我对源代码及运行结果的理解:

    在执行catch()语句时,调用了Systemexitint)方法正常退出,程序就终止了,就不会执行下面的finally语句,所以finally虽用来殿后但并不必须执行。

  • 相关阅读:
    andorid jar/库源码解析之Butterknife
    JavaScript DOM 鼠标拖拽
    JavaScript JSON 与 AJAX
    JavaScript DOM 事件模型
    JavaScript DOM 样式操作
    JavaScript DOM 常用尺寸
    JavaScript 日期与计时器
    JavaScript DOM 基础
    JavaScript 数组
    JavaScript 对象拷贝
  • 原文地址:https://www.cnblogs.com/123456www/p/9944127.html
Copyright © 2011-2022 走看看