首页 > 其他分享 >动手动脑:多个嵌套的try…catch…finally时,finally的执行时机。

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

时间:2022-10-24 17:37:36浏览次数:58  
标签:Level System try println finally result catch out

运行

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语句块执行顺序

标签:Level,System,try,println,finally,result,catch,out
From: https://www.cnblogs.com/ashuai123/p/16822165.html

相关文章

  • 动手动脑:finally语句块一定会被运行吗
    运行packagedemo;publicclassDemo2{ publicstaticvoidmain(String[]args){ try{ System.out.println("inmain");......
  • C++中的try和catch
    程序运行时常会碰到一些异常情况,例如:做除法的时候除数为0;用户输入年龄时输入了一个负数;用new运算符动态分配空间时,空间不够导致无法分配;访问数组元素时,下标越界;打......
  • python retry装饰器
    fromfunctoolsimportwrapsimporttime#defretry(retry_time,retry_on_result,time_wait):#deftest(func):#@wraps(func)#defwrapp......
  • java - allators混淆打包报错[ERROR] STORED entry missing size, compressed size, o
    1.原因这是版本低导致的,低版本与高版本的zip包依赖冲突2.解决官网下载新版的即可,我这里使用8.3版本地址https://allatori.com/点击那个大按钮即可下载最新版本 ......
  • 学习笔记:python语句try
    python学习使用try在进行读取用户输入时,如果我们想读取一个整型,而用户进行了错误的输入,那么程序便会出错,或者当我们做商时,除数为0;这时便可以采用该代码块,来避免程序报错......
  • 替代try catch处理异常的优雅方式
    前言软件开发过程中,不可避免的是需要处理各种异常,就我自己来说,至少有一半以上的时间都是在处理各种异常情况,所以代码中就会出现大量的​​try{…}catch{…}finally{…}......
  • 云服务器安装Mysql之后,设置可以进行远程连接,Duplicaticate wntry '%-root' for key '
    云服务器安装Mysql之后,设置可以进行远程连接1、首先连接mysqlmysql-uroot-p2、MySql5版本GRANTALLON*.*TOroot@'%'IDENTIFIEDBY'你得root密码'WITHGRANT......
  • try catch
    try{    this.submit_data.forEach((item)=>{     for(constkeyinitem){      if(key==="lyName"||key==="ltId"||ke......
  • Java中Final、 finally 、finalize的区别
    1、final可以修饰类、变量、方法,修饰类表示该类不能被继承、修饰方法表示该方法不能被重写、修饰变量表示该变量是一个常量不能被重新赋值。2、finally一般作用在try-catch......
  • ReetranLock锁的trylock与lock方法
    trylock就是尝试加锁,为非阻塞加锁,如果加上就返回true如果失败就返回false这样可以通过trylock写一个自旋锁,写一个while语句尝试给某个线程加锁,直到加上之后返回true就结束......