Java异常注意易错点

本文深入解析Java异常处理机制,详细阐述try、catch、finally的执行流程及常见易错点,通过多个实例演示不同场景下异常处理的行为,帮助读者掌握异常处理的正确用法。

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

Java异常注意易错点

java异常流程执行图:如下图:
在这里插入图片描述
Finally通常会配合try、catch使用,在每一处的try或catch将要退出该方法之前,JVM都会保证先去调用finally的代码,这里所说的退出不单单是指return语句,try或catch中异常的抛出也会导致相应方法的退出(当然,前提是不被catch捕获以及不被finally跳转)。在执行finally代码时,如果finally代码本身没有退出的语句(return或抛出异常),finally执行完毕后还会返回try或catch,由try或catch执行退出指令。

如下代码所示:

public class Test {
	static int f1() {  
        try {  
            return 1;  
        } finally {  
            System.out.print("f1");  
        }  
    }  
  
    static int f2() {  
        try {  
            throw new Exception("try error");  
        } catch (Exception e) {  
            return 2;  
        } finally {  
            System.out.print("f2");  
        }  
    }  
  
    static int f3() {  
        try {  
            throw new RuntimeException("try error");  //当出现异常,但异常没有被捕获时,要先执行finally
        } catch (ArithmeticException e) {  
            return 3;  
        } finally {  
            System.out.print("f3");  
        }  
    }  
  
    static int f4() {  
        try {  
            throw new Exception("try error");  
        } catch (Exception e) {  
            throw new RuntimeException("catch error");  
        } finally {  
            System.out.print("f4");  
        }  
    }  
  
    static int f5() {  
        try {  
            throw new Exception("try error");  
        } catch (Exception e) {  
            throw new RuntimeException("catch error");  
        } finally {  
            System.out.print("f5");  
            return 5;  
        }  
    }  
      
    static int f6() {  
        try {  
            throw new Exception("try error");  
        } catch (Exception e) {  
            throw new RuntimeException("catch error");  
        } finally {  
            System.out.print("f6");  
            throw new RuntimeException("finally error");  
        }  
    }  
    
    static String f7(){
    	try{
    		return "return";
    	}catch(Exception e){
    		System.out.print("catch");
    	}finally{
    		return "finally";
    	}
    }
  
    static String f8(){
    	try{
    		throw new Exception();
    	}catch(Exception e){
    		System.out.print("catch");
    	}finally{
    		return "finally";
    	}
    }
    
    static String f9(){
    	try{
    		throw new Exception();
    		//这个后面不能再写语句,否则会报错
    	}catch(Exception e){
    		return "catch";
    	}finally{
    		return "finally";
    	}
    }
    
    static String f10(){
    	try{
    		System.out.print(" hhh ");
    		System.out.print(1/0);//异常
    		System.out.print(" xxx ");//异常后面的方法不执行
    	}catch(Exception e){
    		System.out.print("catch");
    		System.out.print(" sss ");  //当异常被捕获时,要先执行catch,再执行finally
    	}finally{
    		return "finally";
    	}
    }
    
    
    
    public static void main(String[] args) {  
        System.out.println(" : " + f1());  //结果:f1:1
          
        try {  
            System.out.println(" : " + f2());  
        } catch (Exception e) {  
            System.out.println(" : " + e.getMessage());  //f2:2
        }  
  
        try {  
            System.out.println(" : " + f3());  
        } catch (Exception e) {  
            System.out.println(" : " + e.getMessage());  //f3:try error
        }  
          
        try {  
            System.out.println(" : " + f4());  
        } catch (Exception e) {  
            System.out.println(" : " + e.getMessage());  //f4:catch error
        }  
          
        try {  
            System.out.println(" : " + f5());  
        } catch (Exception e) {  
            System.out.println(" : " + e.getMessage());  //f5:5
        }  
  
        try {  
            System.out.println(" : " + f6());  
        } catch (Exception e) {  
            System.out.println(" : " + e.getMessage());  //f6:finally error
        }  
        
        try {  
            System.out.println(" : " + f7());  
        } catch (Exception e) {  
            System.out.println(" : " + e.getMessage());  // : finally
        }
        
        try {  
            System.out.println(" : " + f8());  
        } catch (Exception e) {  
            System.out.println(" : " + e.getMessage());  //catch : finally
        } 
        
        try {  
            System.out.println(" : " + f9());  
        } catch (Exception e) {  
            System.out.println(" : " + e.getMessage());  // : finally
        } 
        
        System.out.println(f10());
    }     
}

结果为:
f1 : 1
f2 : 2
f3 : try error
f4 : catch error
f5 : 5
f6 : finally error
 : finally
catch : finally
 : finally
 hhh catch sss finally

注意点:
catch和finally可以同时省略吗?
不可以同时省略,否则会报错。
try语句后面是可以省略catch语句的,但必须要有finally语句;也可以省略fnally语句,但必须要有catch语句。也就是说try语句后面必须要有一个别的语句跟在后面。
有以下三种形式:
try catch
try finally
try catch finally

参考自:Java解惑之try catch finally

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值