Java异常处理(try、catch、finally使用)

直接上代码,先贴下面测试须要调用的方法:java

public static void enterTryMethod() {  
    System.out.println("enter after try field");  
}  
  
public static void enterExceptionMethod() {  
    System.out.println("enter catch field");  
}  
  
public static void enterFinallyMethod() {  
    System.out.println("enter finally method");  
}

1. 抛出Exception,没有finally,当catch赶上returnweb

public static int catchTest() {  
    int res = 0;  
      
    try {  
        res = 10 / 0; // 抛出Exception,后续处理被拒绝  
        enterTryMethod();  
        return res; // Exception已经抛出,没有得到被执行的机会  
    } catch (Exception e) {  
        enterExceptionMethod();  
        return 1;   // Exception抛出,得到了调用方法并返回方法值的机会  
    }  
}

后台输出结果:测试

enter catch field  
1

2. 抛出Exception,当catch体里有return,finally体的代码块将在catch执行return以前被执行spa

public static int catchTest() {  
    int res = 0;  
      
    try {  
        res = 10 / 0; // 抛出Exception,后续处理被拒绝  
        enterTryMethod();  
        return res; // Exception已经抛出,没有得到被执行的机会  
    } catch (Exception e) {  
        enterExceptionMethod();  
        return 1;   // Exception抛出,得到了调用方法并返回方法值的机会  
    } finally {  
        enterFinallyMethod(); // Exception抛出,finally代码将在catch执行return以前被执行  
    }  
}

后台输出结果:code

enter catch field  
enter finally method  
1

3. 不抛出Exception,当finally代码块里面赶上return,finally执行完后将结束整个方法orm

public static int catchTest() {  
    int res = 0;  
      
    try {  
        res = 10 / 2; // 不抛出Exception  
        enterTryMethod();  
        return res; // 得到被执行的机会,但执行须要在finally执行完成以后才能被执行  
    } catch (Exception e) {  
        enterExceptionMethod();  
        return 1;  
    } finally {  
        enterFinallyMethod();  
        return 1000; // finally中含有return语句,这个return将结束这个方法,不会在执行完以后再跳回try或者catch继续执行,方法到此结束  
    }  
}

后台输出结果:it

enter after try field  
enter finally method  
1000

4. 不抛Exception,当finally代码块里面赶上System.exit()方法将结束和终止整个程序,而不仅是方法io

public static int catchTest() {  
    int res = 0;  
      
    try {  
        res = 10 / 2; // 不抛出Exception  
        enterTryMethod();  
        return res; // 得到被执行的机会,但因为finally已经终止程序,返回值没有机会被返回  
    } catch (Exception e) {  
        enterExceptionMethod();  
        return 1;  
    } finally {  
        enterFinallyMethod();  
        System.exit(0); // finally中含有System.exit()语句,System.exit()将退出整个程序,程序将被终止  
    }  
}

后台输出结果:class

enter after try field  
enter finally method

5. 抛出Exception,当catch和finally同时赶上return,catch的return返回值将不会被返回,finally的return语句将结束整个方法并返回import

public static int catchTest() {  
    int res = 0;  
      
    try {  
        res = 10 / 0; // 抛出Exception,后续处理将被拒绝  
        enterTryMethod();  
        return res; // Exception已经抛出,没有得到被执行的机会  
    } catch (Exception e) {  
        enterExceptionMethod();  
        return 1; // Exception已经抛出,得到被执行的机会,但返回操做将被finally截断  
    } finally {  
        enterFinallyMethod();  
        return 10;  // return将结束整个方法,返回值为10  
    }  
}

后台输出结果:

enter catch field  
enter finally method  
10

6. 不抛出Exception,当finally赶上return,try的return返回值将不会被返回,finally的return语句将结束整个方法并返回

public static int catchTest() {  
    int res = 0;  
      
    try {  
        res = 10 / 2; // 不抛出Exception  
        enterTryMethod();  
        return res; // 得到执行机会,但返回将被finally截断  
    } catch (Exception e) {  
        enterExceptionMethod();  
        return 1;  
    } finally {  
        enterFinallyMethod();  
        return 10;  // return将结束整个方法,返回值为10  
    }  
}

后台输出结果:

enter after try field  
enter finally method  
10

 

结论

Java的异常处理中,程序执行完try里面的代码块以后,该方法并不会当即结束,而是继续试图去寻找该方法有没有finally的代码块

  • 若是没有finally代码块,整个方法在执行完try代码块后返回相应的值来结束整个方法

  • 若是有finally代码块,此时程序执行到try代码块里的return一句之时并不会当即执行return,而是先去执行finally代码块里的代码

若finally代码块里没有return或没有可以终止程序的代码,程序在执行完finally代码块代码以后再返回try代码块执行return语句来结束整个方法。若 finally 代码块里有 return 或含有可以终止程序的代码,方法将在执行完 finally 以后被结束,再也不跳回 try 代码块执行 return

在抛出异常的状况下,原理也是和上面的同样的,你把上面说到的 try 换成 catch 去理解就OK了。

相关文章
相关标签/搜索