java之try与finally话语

本文通过三个具体的Java示例,详细解析了try、catch、finally语句块在不同情况下的执行流程,包括异常被捕获后的输出顺序及finally块的执行机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如下面的代码,结果就不解释了。

 

Java代码 复制代码
  1. public class TryTest    
  2. {   
  3.     public static void main(String[] args)   
  4.     {   
  5.         TryTest tt = new TryTest();   
  6.            
  7.         tt.test1();   
  8.            
  9.         System.out.println();   
  10.            
  11.         tt.test2();   
  12.            
  13.         System.out.println();   
  14.            
  15.         tt.test3();   
  16.     }   
  17.        
  18.     public void test1()   
  19.     {   
  20.         int a = 2;   
  21.            
  22.         System.out.println("start");   
  23.            
  24.         try  
  25.         {   
  26.             a = a/0;   
  27.                
  28.         }catch(Exception e)   
  29.         {   
  30.             System.out.println("catch");   
  31.                
  32.         }finally  
  33.         {   
  34.             System.out.println("finally");   
  35.         }   
  36.            
  37.         System.out.println("end");   
  38.     }   
  39.        
  40.     public void test2()   
  41.     {   
  42.         int a = 2;   
  43.            
  44.         System.out.println("start");   
  45.            
  46.         try  
  47.         {   
  48.             a = a/0;   
  49.                
  50.         }catch(Exception e)   
  51.         {   
  52.             System.out.println("catch");   
  53.                
  54.             return;   
  55.                
  56.         }finally  
  57.         {   
  58.             System.out.println("finally");   
  59.         }   
  60.            
  61.         System.out.println("end");   
  62.     }   
  63.        
  64.     public void test3()   
  65.     {   
  66.         int a = 2;   
  67.            
  68.         System.out.println("start");   
  69.            
  70.         try  
  71.         {   
  72.             a = a/0;   
  73.                
  74.         }catch(Exception e)   
  75.         {   
  76.             System.out.println("catch");   
  77.                
  78.             System.exit(0);   
  79.                
  80.         }finally  
  81.         {   
  82.             System.out.println("finally");   
  83.         }   
  84.            
  85.         System.out.println("end");   
  86.     }   
  87.        
  88.     public void test()   
  89.     {   
  90.         //只有final可以使用   
  91.         final int a = 1;   
  92. //      static int b = 2;   
  93. //      final static int c = 3;   
  94. //      private int d = 4;   
  95.     }   
  96. }  
public class TryTest 
{
	public static void main(String[] args)
	{
		TryTest tt = new TryTest();
		
		tt.test1();
		
		System.out.println();
		
		tt.test2();
		
		System.out.println();
		
		tt.test3();
	}
	
	public void test1()
	{
		int a = 2;
		
		System.out.println("start");
		
		try
		{
			a = a/0;
			
		}catch(Exception e)
		{
			System.out.println("catch");
			
		}finally
		{
			System.out.println("finally");
		}
		
		System.out.println("end");
	}
	
	public void test2()
	{
		int a = 2;
		
		System.out.println("start");
		
		try
		{
			a = a/0;
			
		}catch(Exception e)
		{
			System.out.println("catch");
			
			return;
			
		}finally
		{
			System.out.println("finally");
		}
		
		System.out.println("end");
	}
	
	public void test3()
	{
		int a = 2;
		
		System.out.println("start");
		
		try
		{
			a = a/0;
			
		}catch(Exception e)
		{
			System.out.println("catch");
			
			System.exit(0);
			
		}finally
		{
			System.out.println("finally");
		}
		
		System.out.println("end");
	}
	
	public void test()
	{
		//只有final可以使用
		final int a = 1;
//		static int b = 2;
//		final static int c = 3;
//		private int d = 4;
	}
}

 

 

输出为:

 

Java代码 复制代码
  1. start   
  2. catch  
  3. finally  
  4. end   
  5.   
  6. start   
  7. catch  
  8. finally  
  9.   
  10. start   
  11. catch 
  12. 我的异常网
    Java Exception
    Dotnet Exception
    Oracle Exception
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值