java基础之异常

###java异常总结构
image

###异常用法:
##无return关键字:
####1:try-catch结构

 try {
      System.out.println("before....");
      int i = 1 / 0;
      System.out.println("behind....");
 } catch (ArrayIndexOutOfBoundsException e) {
      e.printStackTrace();
 }
结果:
before
异常信息

try语句块中出现异常时,异常点后的代码全部不执行,直接跳转到catch进行异常类型匹配,异常类型匹配成功,执行catch代码块中的代码,反之不执行;全部匹配不成功,最终把异常交给JVM处理。
------------------------------------------------------------------
反编译上述代码:
	try
        {
            System.out.println("before....");
            int i = 1 / 0;
            System.out.println("behind....");
        }
        catch(ArrayIndexOutOfBoundsException arrayindexoutofboundsexception)
        {
            arrayindexoutofboundsexception.printStackTrace();
        }

2、try-catch-finally

 	try {
            System.out.println("before....");
            int i = 1 / 0;
            System.out.println("behind....");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally...");
        }

结果:
	before....
	打印异常信息
	finally...
(打印的顺序也可能会是)
	before....
	finally...
	打印异常信息
	

finally语句块中的代码必执行,当出现finally关键字时,jvm在编译期间会根据语法树进行“代码重排序”。

反编译后代码如下:
正常    System.out.println("before....");
        int i = 1 / 0;
        System.out.println("behind....");
        System.out.println("finally...");
结束     break MISSING_BLOCK_LABEL_58;
		
出现异常 Exception exception;
        exception;
        exception.printStackTrace();
        System.out.println("finally...");
结束    break MISSING_BLOCK_LABEL_58;
        
出现异常 Exception exception1;
        exception1;
        System.out.println("finally...");
结束    throw exception1;

从反编译结果可以看到,出现finally关键字,jvm在编译期间会把try-catch-finally结构的代码根据语法树进行重新排序,并且最后的一个异常一定是Exception。


验证:(只捕获IndexOutOfBoundsException)
   try {
            System.out.println("before....");
            int i = 1 / 0;
            System.out.println("behind....");
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally...");
        }

反编译结果如下:
 	    System.out.println("before....");
        int i = 1 / 0;
        System.out.println("behind....");
        System.out.println("finally...");
        break MISSING_BLOCK_LABEL_58;
        
		Exception exception;   -----------IndexOutOfBoundsException
        exception;
        exception.printStackTrace();
        System.out.println("finally...");
        break MISSING_BLOCK_LABEL_58;
        
		Exception exception1;
        exception1;
        System.out.println("finally...");
        throw exception1;

##有return关键字:

1、try-catch
 public static void main(String[] args) {

        System.out.println(met());
    }

    public static String met(){
        try {
            System.out.println("before....");
            int i = 1 / 0;
            System.out.println("behind....");
            return "try";
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
            return "catch";
        }
    }

结果:
	before....
	异常信息  
	
为何没打印出catch ?  异常匹配失败 

代码反编译如下:
	public static void main(String args[]){
        System.out.println(met());
    }

    public static String met(){
        try
        {
            System.out.println("before....");
            int i = 1 / 0;
            System.out.println("behind....");
            return "try";
        }
        catch(ArrayIndexOutOfBoundsException arrayindexoutofboundsexception)
        {
            arrayindexoutofboundsexception.printStackTrace();
        }
        return "catch";
    }

jvm会把catch语句块中的return 提到外面。

上述代码  try语句块中出现异常,catch 进行异常匹配,
ArrayIndexOutOfBoundsException类型异常与之不匹配,交给jvm进行处理。
2、try-catch-finally
 	public static void main(String[] args) {

        System.out.println(met());
    }

    public static String met() {
        try {
            System.out.println("before....");
            int i = 1 / 0;
            System.out.println("behind....");
            return "try";
        } catch (Exception e) {
            e.printStackTrace();
            return "catch";
        } finally {
            System.out.println("finally...");
            return "finally";
        }
    }

结果:
 before....
 异常信息
 finally...
 finally
 或者
 before....
 finally...
 finally
 异常信息
 
 反编译代码
 public class T_003
 {
     public T_003(){
     }

     public static void main(String args[]){
        System.out.println(met());
     }

     public static String met(){
 try正常: System.out.println("before....");
         int i = 1 / 0;
         System.out.println("behind....");
         String s = "try";
         System.out.println("finally...");
         return "finally";

catch:   Exception exception;
         exception;
         exception.printStackTrace();
         String s1 = "catch";
         System.out.println("finally...");
         return "finally";


catch:   Exception exception1;
         exception1;
         System.out.println("finally...");
         return "finally";
    }
 }

####根据上述例子总结如下执行流程。
image
参考资料
http://www.wjhsh.net/wpbxin-p-14618750.html
https://www.pdai.tech/md/java/basic/java-basic-x-exception.html

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值