第8周动手动脑

1.多层的异常捕获-1

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"); 
        } 
    } 
}

两个try-catch异常捕获,第一个throw抛出的错误,被内层catch捕获,故最后一个catch未捕获,不显示;第二个catchArithmeticException,被同名即第二个catch捕获,显示发生ArithmeticException。

多层的异常捕获-2

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"); 
        } 
    } 
}

当第一个throw抛出错误后,直接跳转到最后一个同名的catch捕获块,中间程序未运行。故总结,Java中,使用try-catch语法,一旦出错,就捕获该错误;若注销第一个throw错误,则会运行第二个catch,显示发生ArithmeticException。

2.Embedefinally

public class EmbededFinally {

    
    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嵌套,每个try、catch、finally均有输出语句。输出顺序为从第一个try开始执行三次,catch仅执行最里层level3,finally从最里层向外执行。

Finally主要用于解决资源泄露问题,它位于catch语句块后,JVM保证它一定执行,因此从最里层执行,毫无疑问。

由于finally语块中可能发生异常,比如此处的level3就发生java.lang.ArithmeticException异常,一旦发生此种异常,先前异常就会被抛弃,故仅仅最里层的catch捕获到异常,之后由于异常被抛弃,level2、level3的catch并未捕捉到异常不显示。

另外根据try-catch方法使用,try语句块一有异常,则找相应catch捕获经验得知,三个try中均为异常错误,故依次执行try中语句块。

3.SystemExitAndFinally

public class SystemExitAndFinally {

    
    public static void main(String[] args)
    {
        
        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");
        
        }
    
    }


}

通常情况下,finally运行语句一定执行,但本题中有特殊情况,在catch中有“System.exit(0);”执行此语句后,就已经结束程序,故不会运行finally语句。

4.

猜你喜欢

转载自www.cnblogs.com/zlc364624/p/9942895.html