###java异常总结构
###异常用法:
##无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";
}
}
####根据上述例子总结如下执行流程。
参考资料
http://www.wjhsh.net/wpbxin-p-14618750.html
https://www.pdai.tech/md/java/basic/java-basic-x-exception.html