我们看如下代码:

@Slf4j
public class Test {
public static void main(String[] args) {
try {
try {
test();
} finally {
test2();
}
} catch (Exception e) {
System.out.println(e.getMessage());
} } public static void test() {
System.out.println("test方法执行了");
throw new RuntimeException("veryImportant exception");
} public static void test2() {
System.out.println("test2");
throw new CustomeException("common exception");
}
}

执行结果如下:


我们可以看到,在执行的适合,一个veryImportant exception丢失了,而抛出了一个common exception,这是相当严重的缺陷,因为异常可能会以一种比前面例子所示更微妙和难以察觉的方式完全丢失。相比之下,C++把“前一个异常还没处理就抛出下一个异常”的情形看成是糟糕的错误。

一种更加糟糕的编程手法如下所示:

package com.study.spring.transaction.controller;

import com.study.spring.transaction.CustomeException;
import lombok.extern.slf4j.Slf4j; /**
* @Author: dmz
* @Description:
* @Date: Create in 2:40 2019/4/21
*/
@Slf4j
public class Test {
public static void main(String[] args) {
try {
try {
test();
} finally {
return;
}
} catch (Exception e) {
System.out.println(e.getMessage());
} } public static void test() {
System.out.println("test方法执行了");
throw new RuntimeException("veryImportant exception");
} public static void test2() {
System.out.println("test2");
throw new CustomeException("common exception");
}
}

可以看到,在finally快中,我们直接将函数返回了,这个适合执行代码,会发现,即使抛出了异常,也不会有任何输出。

这种情况下我们怎么办呢?我觉得主要就是多加日志,虽然程序不会主动输出什么,但是我们可以在出现错误的地方自己打印日志

@Slf4j
public class Test {
public static void main(String[] args) {
try {
try {
test2();
} finally {
return;
}
} catch (Exception e) {
System.out.println(e.getMessage());
} } public static void test() {
System.out.println("test方法执行了");
throw new RuntimeException("veryImportant exception");
} public static void test2() {
System.out.println("test2");
CustomeException common_exception = new CustomeException("common exception");
log.error("test2调用失败", common_exception);
throw common_exception;
}

调用结果:

test2
00:46:09.508 [main] ERROR com.study.spring.transaction.controller.Test - test2调用失败
com.study.spring.transaction.CustomeException: common exception
at com.study.spring.transaction.controller.Test.test2(Test.java:33)
at com.study.spring.transaction.controller.Test.main(Test.java:16) Process finished with exit code 0

最新文章

  1. Hanio汉诺塔代码递归实现
  2. ASP.NET MVC3 中整合 NHibernate3.3、Spring.NET2.0 使用AOP执行事务处理
  3. Intellij IDEA中配置Maven
  4. java使用thrift
  5. Android---WebView显示Html乱码问题
  6. MySQL之左连接与右连接
  7. excel用vlookup,lookup时查询数据需要排序
  8. vm安装mac系统
  9. 几种RAID技术比较
  10. 我的github今天大手笔分享,welcome——fork
  11. UML类图常见的几种关系
  12. Spring实战笔记2---Bean的装配
  13. hdu4570Multi-bit Trie (间隙DP)
  14. memcached+tomcat转发forward时 sessionid一直变化的问题
  15. 机器学习——决策树,DecisionTreeClassifier参数详解,决策树可视化查看树结构
  16. 高并发系统保护~ing
  17. [ASP.NET]ScriptManager控件使用 转载
  18. Java 获取窗口的宽、高
  19. 深入理解Java中的同步静态方法和synchronized(class)代码块的类锁
  20. 开启远程Windows系统3389端口

热门文章

  1. 【一统江湖的大前端(9)】TensorFlow.js 开箱即用的深度学习工具
  2. 知识点二:HTTP超文本文件传输协议
  3. unity3d之简单动画
  4. Sprint 5 summary: UI 界面更新,Azure端部署和用户反馈分析 12/28/2015
  5. A 蚂蚁觅食
  6. ant-design-vue表单生成组件form-create快速上手
  7. Python 代码编辑器怎么选?PyCharm、VS Code、Jupyter Notebook 都各有特色
  8. 立体匹配-----NCC视差匹配
  9. pysparnn 模块使用,相似句子召回
  10. JDBC 中的事务和批处理 batch