前言:
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. Ajax入门(一)

    最近开始学习Ajax了0.0,虽然其他的还没巩固,但既然学了就先写下来吧... Ajax的定义呢,百度吧= =...就觉得最实用的地方就是页面无刷新,但网页实现了与后台的同步更新.   1, 首先创建 ...

  2. 分分钟搞定IOS远程消息推送

    一.引言 IOS中消息的推送有两种方式,分别是本地推送和远程推送,本地推送在http://my.oschina.net/u/2340880/blog/405491这篇博客中有详细的介绍,这里主要讨论远 ...

  3. CentOS 7.0 使用 yum 安装 MariaDB 与 MariaDB 的简单配置

    1.安装MariaDB 安装命令 yum -y install mariadb mariadb-server 安装完成MariaDB,首先启动MariaDB,两条命令都可以 systemctl sta ...

  4. Ubuntu 上搭建 Samba 服务器

    由于经常要接收同事发送的一些文件,U盘拷来拷去的很麻烦. 在本机Ubuntu上搭了各Samba服务器,过程中遇到点小问题,记录一下 sudo apt-get install samba 创建一个共享目 ...

  5. 加快XCode的编译链接速度(200%+)—XCode编译速度慢的解决方案

    最近在开发一个大项目的时候遇到一个很头疼的问题,由于项目代码较多,每次都要编译链接1分钟左右,调试的时候很浪费时间,于是研究了一下如何提高编译链接的速度,在这里分享给大家. 提升编译链接的速度主要有以 ...

  6. ajax实现局部刷新

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  7. iOS dealloc 不被调用的问题

    最近项目中老是无缘无故的出现一下奔溃.查看一下堆栈信息,指针的指向都有,但还是奔溃.所以第一个怀疑出现野指针引起的问题.然后调试代码中的一下dealloc函数.好多对象在释放之后都没掉用.顿时吓出一身 ...

  8. [信安presentation]Fight against GFW

    Section1:加密 加密算法分为:对称加密算法.非对称加密算法.Hash 1.1对称加密算法 加密解密使用相同的密钥 eg:DES,AES,RC4,RC5,Triple DES 缺点:1.因为加密 ...

  9. python 之 logging

    #coding=utf-8 import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename ...

  10. Linux--Tail命令

    inux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新,tail会自己主动刷新,确保你看到最新的档案 ...