前言:
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. QDialog QMainwindow QWidget QFrame不同时候用法.

    继承关系:在Qt中所有的类都有一个共同的基类QObject ,QWidget直接继承与QPaintDevice类,QDialog.QMainWindow.QFrame直接继承QWidget 类.   ...

  2. 工作邮件loop的用法

    examples come from native speaker Put john in the loop about this. He will have good advice. Why hav ...

  3. Nfs+Drdb+Heartbeat 数据存储高可用服务架构方案

    一.方案的应用场景 适用于2千万-3千万PV架构的网站,Nfs数据存储高可用服务方案 备注:互联网排名前30左右公司常用的架构 二.生产环境方案部署原理图 三.生产环境服务器硬件配置: 生产环境中采用 ...

  4. EncodingHelper

    /// <summary> /// Url解码 /// </summary> /// <param name="str">原始字符串</p ...

  5. 修改/etc/profile和/etc/environment导致图形界面无法登陆的问题

    在使用ubuntu开发时,往往要修改PATH变量,有时会通过修改/etc/profile和/etc/environment来修改默认的PATH变量,但是一旦出错,很容易造成无法登陆进入图形界面的问题. ...

  6. 关于delphi7的四舍五入

    round 函数是银行用的 采用了 四舍六入5留偶 网上找到了个实现方法   先乘1000,用Trunc取整,除10取余,余数再取整,如果大于5,进位,小于5不进位. 函数就好写了 现在只写一个保留两 ...

  7. Django知识点整理

    什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. web应用 访 ...

  8. easyUI combobox 控件 使用

    1,combobox 二级联动 (参数传递 函数调用) // toolbar 学校班级二级联动 var paramSearch = { school : $('#search-school'), cl ...

  9. web api9

  10. 利用BitMap进行大数据排序去重

    1.问题 问题提出: M(如10亿)个int整数,只有其中N个数重复出现过,读取到内存中并将重复的整数删除. 2.解决方案 问题分析: 我们肯定会先想到在计算机内存中开辟M个int整型数据数组,来on ...