动手动脑:多个嵌套的try…catch…finally时,finally的执行时机。

运行

package demo;


public class Demo2 {

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

}

  

结果

in Level 1
in Level 2
in Level 3
Level 3:class java.lang.ArithmeticException
In Level 3 finally
In Level 2 finally
In Level 1 finally

  

更改错误顺序

package demo;


public class Demo2 {

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

}

  

结果

in Level 1
in Level 2
Level 2:class java.lang.ArithmeticException
In Level 2 finally
In Level 1 finally

  

在更改错误位置

package demo;


public class Demo2 {

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

}

  
结果

in Level 1
in Level 2
in Level 3
In Level 3 finally
Level 2:class java.lang.ArithmeticException
In Level 2 finally
In Level 1 finally

  

再次更改错误位置

package demo;

public class Demo2 {

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

		}

	}

}

  

结果

in Level 1
in Level 2
in Level 3
In Level 3 finally
In Level 2 finally
Level 1:class java.lang.ArithmeticException
In Level 1 finally

  

当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序

posted @ 2022-10-24 17:22  ashuai~  阅读(269)  评论(0)    收藏  举报