Java异常处理

很多刚接触Java的人不太理解Java的异常处理的用法,搞不清楚为啥有这个东西。

其实简单点说,异常就是控制流程的转意。没有特别的地方,可以在方法的任何地方抛出异常,也可以处理异常。Java里面的异常一般用throw,try…catch关键词。

下面是一个简单的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class T{
public void m2(){
}
public void he(Exception ex){
}
    public void m1(){
        try{
            m2();
        }catch(RuntimeException ex){
            he(ex);
        }
        catch(Exception ex){
            he(ex);
        }
    }
}

再看看这段代码的Class指令:

Compiled from "T.java"
public class T extends java.lang.Object{
public T();
  Code:
   0:	aload_0
   1:	invokespecial	#1; //Method java/lang/Object."":()V
   4:	return
 
public void m2();
  Code:
   0:	return
 
public void he(java.lang.Exception);
  Code:
   0:	return
 
public void m1();
  Code:
   0:	aload_0          //load this object....
   1:	invokevirtual	#2; //Method m2:()V  执行m2()方法
   4:	goto	22       //如果有异常就跳转。。
   7:	astore_1        //保存RuntimeException类型临时变量,异常表中会用到
   8:	aload_0         //load this object........
   9:	aload_1        //load #7的临时变量
   10:	invokevirtual	#4; //Method he:(Ljava/lang/Exception;)V
   13:	goto	22    
   16:	astore_1
   17:	aload_0
   18:	aload_1
   19:	invokevirtual	#4; //Method he:(Ljava/lang/Exception;)V
   22:	return
  Exception table:       // 异常表。
   from   to  target type
     0     4     7   Class java/lang/RuntimeException   
     0     4    16   Class java/lang/Exception
}

Java里面的异常就是控制流程的转移,没有别的东西。没有那么豪华。。。

程序中如果增加了关键词finally,指令集变得『复杂』起来。finally关键词在语言层面表示无论怎样都要被执行的意思。

还以上面的java代码为例:

public class T{
public void m2(){
}
public void he(Exception ex){
}
    public void m1(){      
        try{
            m2();
        }catch(RuntimeException ex){
            he(ex);
        }
        catch(Exception ex){
            he(ex);
        }finally{
            System.out.println("Hello");  //无论如何都打印这个字符串。
        }
    }
}

Class指令如下:

Compiled from "T.java"
public class T extends java.lang.Object{
public T();
  Code:
   0:	aload_0
   1:	invokespecial	#1; //Method java/lang/Object."":()V
   4:	return
 
public void m2();
  Code:
   0:	return
 
public void he(java.lang.Exception);
  Code:
   0:	return
 
public void m1();
  Code:
   0:	aload_0
   1:	invokevirtual	#2; //Method m2:()V
   4:	getstatic	#3; //Field java/lang/System.out:Ljava/io/PrintStream;
   7:	ldc	#4; //String Hello
   9:	invokevirtual	#5; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   12:	goto	60
   15:	astore_1
   16:	aload_0
   17:	aload_1
   18:	invokevirtual	#7; //Method he:(Ljava/lang/Exception;)V
   21:	getstatic	#3; //Field java/lang/System.out:Ljava/io/PrintStream;
   24:	ldc	#4; //String Hello
   26:	invokevirtual	#5; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   29:	goto	60
   32:	astore_1
   33:	aload_0
   34:	aload_1
   35:	invokevirtual	#7; //Method he:(Ljava/lang/Exception;)V
   38:	getstatic	#3; //Field java/lang/System.out:Ljava/io/PrintStream;
   41:	ldc	#4; //String Hello
   43:	invokevirtual	#5; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   46:	goto	60
   49:	astore_2
   50:	getstatic	#3; //Field java/lang/System.out:Ljava/io/PrintStream;
   53:	ldc	#4; //String Hello
   55:	invokevirtual	#5; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   58:	aload_2
   59:	athrow
   60:	return
  Exception table:
   from   to  target type
     0     4    15   Class java/lang/RuntimeException
 
     0     4    32   Class java/lang/Exception
 
     0     4    49   any
    15    21    49   any
    32    38    49   any
    49    50    49   any
 
}

方法m2()的字节码中0~9,15~26,32~43,49~59 分别表示了程序执行的4种可能结果。字节码变得复杂庞大。。。

分享到: 更多