前言:
java 中的异常处理机制你真的理解了吗?掌握了吗?
catch 体里遇到 return 是怎么处理? finally 体遇到 return 怎么办?finally 体里有 System.exit() 方法怎么处理?当 catch 和 finally 体里同时遇上 return 怎么办?

相信你在处理异常的时候不是每次都把它 throws 掉就完事了,很多时候异常是需要我们自己来 catch 并针对所抛出的 Exception 做一些后续的处理工作。

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

 1
 2    // catch 后续处理工作
 3    public static boolean catchMethod() {
 4        System.out.print("call catchMethod and return  --->>  ");
 5        return false;
 6    }
 7    // finally后续处理工作
 8    public static void finallyMethod() {
 9        System.out.println();
10        System.out.print("call finallyMethod and do something  --->>  ");
11    }
12

1. 抛出 Exception,没有 finally,当 catch 遇上 return

 1
 2public static boolean catchTest() {
 3        try {
 4            int i = 10 / 0;   // 抛出 Exception,后续处理被拒绝
 5            System.out.println("i vaule is : " + i);
 6            return true;    // Exception 已经抛出,没有获得被执行的机会
 7        } catch (Exception e) {
 8            System.out.println(" -- Exception --");
 9            return catchMethod();    // Exception 抛出,获得了调用方法并返回方法值的机会
10        }
11    }
12

后台输出结果:

1
2 -- Exception --
3call catchMethod and return  --->>  false
4

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

 1
 2public static boolean catchFinallyTest1() {
 3        try {
 4            int i = 10 / 0; // 抛出 Exception,后续处理被拒绝
 5            System.out.println("i vaule is : " + i);
 6            return true;   // Exception 已经抛出,没有获得被执行的机会
 7        } catch (Exception e) {
 8            System.out.println(" -- Exception --");
 9            return catchMethod();  // Exception 抛出,获得了调用方法的机会,但方法值在 finally 执行完后才返回
10        }finally{
11            finallyMethod();  // Exception 抛出,finally 代码块将在 catch 执行 return 之前被执行
12        }
13    }
14

后台输出结果:

1
2 -- Exception --
3call catchMethod and return  --->>  
4call finallyMethod and do something  --->>  false
5

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

 1
 2public static boolean catchFinallyTest2() {
 3        try {
 4            int i = 10 / 2;  // 不抛出 Exception
 5            System.out.println("i vaule is : " + i);
 6            return true;   // 获得被执行的机会,但执行需要在 finally 执行完成之后才能被执行
 7        } catch (Exception e) {
 8            System.out.println(" -- Exception --");
 9            return catchMethod();
10        }finally{
11            finallyMethod();
12            return false; // finally 中含有 return 语句,这个 return 将结束这个方法,不会在执行完之后再跳回 try 或 catch 继续执行,方法到此结束,返回 false
13        }
14    }
15

后台输出结果:

1
2i vaule is : 5
3
4call finallyMethod and do something  --->>  false
5

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

 1
 2public static boolean finallyExitTest() {
 3        try {
 4            int i = 10 / 2;  // 不抛出 Exception
 5            System.out.println("i vaule is : " + i);
 6            return true;   // 获得被执行的机会,但由于 finally 已经终止程序,返回值没有机会被返回
 7        } catch (Exception e) {
 8            System.out.println(" -- Exception --");
 9            return true;
10        }finally {
11            finallyMethod();
12            System.exit(0);// finally 中含有 System.exit() 语句,System.exit() 将退出整个程序,程序将被终止
13        }
14    }
15

后台输出结果:

1
2i vaule is : 5
3
4call finallyMethod and do something  --->>  
5

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

 1
 2public static boolean finallyTest1() {
 3        try {
 4            int i = 10 / 0; // 抛出 Exception,后续处理被拒绝
 5            System.out.println("i vaule is : " + i);
 6            return true;   // Exception 已经抛出,没有获得被执行的机会
 7        } catch (Exception e) {
 8            System.out.println(" -- Exception --");
 9            return true;  // Exception 已经抛出,获得被执行的机会,但返回操作将被 finally 截断
10        }finally {
11            finallyMethod();
12            return false;  // return 将结束整个方法,返回 false
13        }
14    }
15

后台输出结果:

1
2 -- Exception --
3
4call finallyMethod and do something  --->>  false
5

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

 1
 2public static boolean finallyTest2() {
 3        try {
 4            int i = 10 / 2;  // 不抛出 Exception
 5            System.out.println("i vaule is : " + i);
 6            return true;   // 获得被执行的机会,但返回将被 finally 截断
 7        } catch (Exception e) {
 8            System.out.println(" -- Exception --");
 9            return true;
10        }finally {
11            finallyMethod();
12            return false; // return 将结束这个方法,不会在执行完之后再跳回 try 或 catch 继续执行,返回 false
13        }
14    }
15

后台输出结果:

1
2i vaule is : 5
3
4call finallyMethod and do something  --->>  false
5

结语:
(假设方法需要返回值)
java 的异常处理中,
在不抛出异常的情况下,程序执行完 try 里面的代码块之后,该方法并不会立即结束,而是继续试图去寻找该方法有没有 finally 的代码块,
如果没有 finally 代码块,整个方法在执行完 try 代码块后返回相应的值来结束整个方法;
如果有 finally 代码块,此时程序执行到 try 代码块里的 return 语句之时并不会立即执行 return,而是先去执行 finally 代码块里的代码,
若 finally 代码块里没有 return 或没有能够终止程序的代码,程序将在执行完 finally 代码块代码之后再返回 try 代码块执行 return 语句来结束整个方法;
若 finally 代码块里有 return 或含有能够终止程序的代码,方法将在执行完 finally 之后被结束,不再跳回 try 代码块执行 return。
在抛出异常的情况下,原理也是和上面的一样的,你把上面说到的 try 换成 catch 去理解就 OK 了 *_*

PS:附上完整代码

public class TestException {

    // catch 后续处理工作
public static boolean catchMethod() {
System.out.print("call catchMethod and return --->> ");
return false;
}
// finally后续处理工作
public static void finallyMethod() {
System.out.println();
System.out.print("call finallyMethod and do something --->> ");
} public static boolean catchTest() {
try {
int i = 10 / 0; // 抛出 Exception,后续处理被拒绝
System.out.println("i vaule is : " + i);
return true; // Exception 已经抛出,没有获得被执行的机会
} catch (Exception e) {
System.out.println(" -- Exception --");
return catchMethod(); // Exception 抛出,获得了调用方法并返回方法值的机会
}
} public static boolean catchFinallyTest1() {
try {
int i = 10 / 0; // 抛出 Exception,后续处理被拒绝
System.out.println("i vaule is : " + i);
return true; // Exception 已经抛出,没有获得被执行的机会
} catch (Exception e) {
System.out.println(" -- Exception --");
return catchMethod(); // Exception 抛出,获得了调用方法的机会,但方法值在 finally 执行完后才返回
}finally{
finallyMethod(); // Exception 抛出,finally 代码块将在 catch 执行 return 之前被执行
}
} public static boolean catchFinallyTest2() {
try {
int i = 10 / 2; // 不抛出 Exception
System.out.println("i vaule is : " + i);
return true; // 获得被执行的机会,但执行需要在 finally 执行完成之后才能被执行
} catch (Exception e) {
System.out.println(" -- Exception --");
return catchMethod();
}finally{
finallyMethod();
return false; // finally 中含有 return 语句,这个 return 将结束这个方法,不会在执行完之后再跳回 try 或 catch 继续执行,方法到此结束,返回 false
}
} public static boolean finallyTest2() {
try {
int i = 10 / 2; // 不抛出 Exception
System.out.println("i vaule is : " + i);
return true; // 获得被执行的机会,但返回将被 finally 截断
} catch (Exception e) {
System.out.println(" -- Exception --");
return true;
}finally {
finallyMethod();
return false; // return 将结束这个方法,不会在执行完之后再跳回 try 或 catch 继续执行,返回 false
}
} public static boolean finallyExitTest() {
try {
int i = 10 / 2; // 不抛出 Exception
System.out.println("i vaule is : " + i);
return true; // 获得被执行的机会,但由于 finally 已经终止程序,返回值没有机会被返回
} catch (Exception e) {
System.out.println(" -- Exception --");
return true;
}finally {
finallyMethod();
System.exit(0);// finally 中含有 System.exit() 语句,System.exit() 将退出整个程序,程序将被终止
}
} public static boolean finallyTest1() {
try {
int i = 10 / 0; // 抛出 Exception,后续处理被拒绝
System.out.println("i vaule is : " + i);
return true; // Exception 已经抛出,没有获得被执行的机会
} catch (Exception e) {
System.out.println(" -- Exception --");
return true; // Exception 已经抛出,获得被执行的机会,但返回操作将被 finally 截断
}finally {
finallyMethod();
return false; // return 将结束整个方法,返回 false
}
} public static void main(String[] args) {
//boolean flag= catchTest();
// boolean flag= catchFinallyTest1();
boolean flag= catchFinallyTest2();
//boolean flag= finallyExitTest();
//boolean flag= finallyTest1();
//boolean flag= finallyTest2(); System.out.println("");
System.out.println("---------flag------"+flag); } }

基础知识《十》java 异常捕捉 ( try catch finally ) 你真的掌握了吗?的更多相关文章

  1. java 异常捕捉 ( try catch finally ) 你真的掌握了吗?

    掌握下面几条原则就可以完全解决“当try.catch.finally遭遇return”的问题. 原则:1.finally语句块中的代码是一定会执行的,而catch块中的代码只有发生异常时才会执行. 2 ...

  2. java基础知识学习 java异常

    1: Unchecked Exception( 也就是运行时异常) VS  Check Exception(非运行时异常) 2: 运行期异常  VS  非运行期异常? 非运行时异常: 必须在代码中显示 ...

  3. 异常捕捉 ( try catch finally ) 你真的掌握了吗?

    前言:java 中的异常处理机制你真的理解了吗?掌握了吗?catch 体里遇到 return 是怎么处理? finally 体遇到 return 怎么办?finally 体里有 System.exit ...

  4. Java基础 -- 深入理解Java异常机制

    异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等.异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程.Java通 过API中Throwable类的众多子类描述各种不同的异常. ...

  5. java 程序运行的基础知识【Java bytecode】

    聊聊文字,写一篇关于 java 基础知识的博文. JVM 线程栈 到 函数运行 每一个JVM线程来说启动的时候都会创建一个私有的线程栈.一个jvm线程栈用来存储栈帧,jvm线程栈和C语言中的栈很类似, ...

  6. 菜鸡的Java笔记 第三十 - java 异常的捕获及处理

    异常的捕获及处理        1.异常的产生分析以及所带来的影响        2.异常的处理的基本格式        3.异常的处理流程        4.异常的处理模式        5.自定义 ...

  7. java 基础知识三 java变量

    java  基础知识 三 变量 1.作用域 {} 包围起来的代码 称之为代码块,在块中声明的变量只能在块中使用 2.常量 就是固定不变的量,一旦被定义,它的值就不能再被改变. 3.变量 变量必须在程序 ...

  8. 《Java基础知识》Java异常处理详解

    1. Java 中的异常 前言:Java 中的异常处理是处理程序运行错误时的强大机制之一,它可以保证应用程序的正常流程. 首先我们将了解java异常.异常的类型以及受查和非受查异常之间的区别. 1.1 ...

  9. java 基础知识十 继承和多态

    java  基础知识十   继承和多态 继承 1.定义: 继承是指声明一些类,可以再进一步声明这些类的子类,而子类具有父类已经拥有的一些方法和属性,这跟现实中的父子关系是十分相似的,所以面向对象把这种 ...

随机推荐

  1. 信息中心网络 ,Information-centric networking, ICN

  2. C#计算代码执行时间

    System.Diagnostics.Stopwatch watch = new Stopwatch();watch.Start(); //要计算的操作 DoSomeThing(); watch.St ...

  3. 0103MySQL中的B-tree索引 USINGWHERE和USING INDEX同时出现

    转自博客http://www.amogoo.com/article/4 前提1,为了与时俱进,文中数据库环境为MySQL5.6版本2,为了通用,更为了避免造数据的痛苦,文中所涉及表.数据,均来自于My ...

  4. AngularJS指令

    1. AngularJS指令的特点: AngularJS通过被称为指令的新属性来扩展HTML,指令的前缀为ng-. AngularJS通过内置的指令来为应用添加功能. AngularJS允许你自定义指 ...

  5. 虚拟机下Centos7如何设置静态IP地址

    最近在学习linux环境部署~~~~ 首先,将网络适配设置成为桥接模式 查看本机IP地址,ipconfig,记住ipv4地址和默认网关地址,等会配置的时候要用 启动Centos,进入终端模式,设置IP ...

  6. poj2217

    Secretary Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1257   Accepted: 515 Descript ...

  7. C#面向对象设计模式纵横谈——2.Singleton 单件(创建型模式)

    一:模式分类 从目的来看: 创建型(Creational)模式:负责对象创建. 结构型(Structural)模式:处理类与对象间的组合. 行为型(Behavioral)模式:类与对象交互中的职责分配 ...

  8. 利用animation和text-shadow纯CSS实现loading点点点的效果

    经常在网上看到loading状态时的点点点的动态效果,自己也用JS写了一个,思路是使用一个计数参数,然后在需要添加点的元素后面利用setInterval一个一个加点,当计数到3时,把点变为一个--写完 ...

  9. 去掉Win7快捷方式小箭头(win10通用)

    我是一个有强迫症的优化控 , 因为近视的缘故 , 喜欢将桌面图标放大 ,  但是win7快捷方式的小箭头 , 确实不好看 . 用win7魔方之类的软件 , 可以解决这个问题 , 但是有时候重启 ,   ...

  10. C# Winform防止一个程序重复运行

    1: //在写一些服务型的软件的时候,你可能不希望一个操作系统里有两个副本在运行,这样也许会扰乱你的操作.这时,你就需要限制程序的副本.下面的这个方法,很简单的就可以实现上述功能. using Sys ...