• try/catch/finally的各种情况


    众所周知,try语句报错,会执行catch语句,然后执行finally,以下这几种情况,看看会如何输出。

    1、try语句中包含return,finally包含输出语句

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. return "111";
    8. } catch (Exception e) {
    9. throw new NullPointerException();
    10. } finally {
    11. System.out.println(123);
    12. }
    13. }

    结果:finally正常输出

     2、try语句报错,catch语句抛出错误,finally包含输出语句

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. throw new NullPointerException();
    8. } catch (Exception e) {
    9. throw new NullPointerException();
    10. } finally {
    11. System.out.println(123);
    12. }
    13. }

    结果:finally执行成功,再抛出catch中的错误

     3、try语句报错,catch包含return,finally包含输出语句

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. throw new NullPointerException();
    8. } catch (Exception e) {
    9. System.out.println(111);
    10. return "456";
    11. } finally {
    12. System.out.println(123);
    13. }
    14. }

    结果:catch中的语句输出成功,finally中的语句输出成功

     4、try和finally中的语句都包含输出

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. return "123";
    8. } catch (Exception e) {
    9. System.out.println(111);
    10. return "456";
    11. } finally {
    12. return "789";
    13. }
    14. }

    结果:finally成功return

     5、try报错,catch和finally包含return语句

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. throw new NullPointerException();
    8. } catch (Exception e) {
    9. System.out.println(111);
    10. return "456";
    11. } finally {
    12. return "789";
    13. }
    14. }

    结果:finally成功return

     6、try包含return,finally抛出错误

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. return "789";
    8. } catch (Exception e) {
    9. System.out.println(111);
    10. return "456";
    11. } finally {
    12. throw new NullPointerException();
    13. }
    14. }

    结果:直接报错

     7、try包含报错,catch包含return,finally抛出错误

    1. public static void main(String[] args) {
    2. // write your code here
    3. System.out.println(get());
    4. }
    5. public static String get(){
    6. try {
    7. throw new RuntimeException();
    8. } catch (Exception e) {
    9. return "456";
    10. } finally {
    11. throw new NullPointerException();
    12. }
    13. }

    结果:直接报错

    通过上面的例子,我们可以得到以下结论:

    1. finally语句存在的话,一定会执行
    2. 若try语句报错,会执行catch语句,如果catch语句也报错,则会先执行finally语句,再抛出catch的错误
    3. try语句或者catch语句return了,finally仍然会执行
    4. 若finally语句中包含return,则会覆盖try或者catch中的return
    5. 若finally语句报错,这个错误一定会抛出,终止程序
  • 相关阅读:
    文章采集器-免费文章采集器
    牛客 HJ28 素数伴侣
    CI/CD 持续集成与持续交付(1)
    单元测试(一):我的第一个单元测试
    一级建造师从业者面试需要注意什么问题?
    django中的自定义分页器
    程序员脱发怎么办
    JavaSE复习总结
    C语言银行综合业务系统
    CleanMyMac X4.14.1最新版本下载
  • 原文地址:https://blog.csdn.net/qq_49721447/article/details/127983337