前言:
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. hihocoder-1453-Rikka with Tree

    #Hihocoder 1453 : Rikka with Tree 时间限制:10000ms 单点时限:1000ms 内存限制:256MB   source: https://hihocoder.co ...

  2. 理解"熵"

    熵描述了事物的混乱程度 一个变量x,它的可取值为x1,x2,x3,x4.当它取值为这几个值时,概率分别为p1,p2,p3,p4.那么这个混乱程度就可以描述为f(p1,p2,p3,p4). 二元取值时熵 ...

  3. 版本管理工具svn简介

    svn简介 SVN是一种C/S架构的版本管理软件 , 能够帮助我们保存开发过程中各个文件的所有历史版本, 你因此可以方便的找回软件的任何一个历史状态., 日常开发中经常用到. 安装使用 1.  在 u ...

  4. Web爬虫入门

    1.0示例学习:Web爬虫 public class WebCrawler { // 种子url private static String url = "http://www.cnblog ...

  5. qrcode 生成验证码带文字

    /** * 生成二维码 * * @param int $id * @param string $file * @param boolean $is_download */public function ...

  6. Node.js API 初解读(一)

    Node.JS API 初解读 Version: NodeJs v6.2.0 一. Assert 1.简介 Assert模块主要用于断言.如果表达式不符合预期,就抛出一个错误. 该模块用于编写程序的单 ...

  7. 移动前端开发之viewport的深入理解

    在移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上的viewport了,只有明白了viewport的概念以及弄清楚了跟viewport有关的meta标签的使用,才能更好地让我们的网页适配或 ...

  8. 解决Myeclipse PermGen space问题

    myeclipse配置web服务器配置 Window—Preferences—Myeclipse—Servers—tomcat JDK的Optional Java VM arguments配置为:-X ...

  9. 关于struts2的过滤器和mybatis的插件的分析

    网上一搜,发现一篇写的非常棒的博文,就直接复制过来了,供以后复习使用. 前辈博文链接:共三篇: http://jimgreat.iteye.com/blog/1616671: http://jimgr ...

  10. [Windows] win7 配置Java开发环境

    Installed Softwares Git for windows 64 bit Java 7 & 8 64 bit apache maven 3.3.3 Intellij Idea ID ...