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