- 浏览: 7862764 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (2425)
- 软件工程 (75)
- JAVA相关 (662)
- ajax/web相关 (351)
- 数据库相关/oracle (218)
- PHP (147)
- UNIX/LINUX/FREEBSD/solaris (118)
- 音乐探讨 (1)
- 闲话 (11)
- 网络安全等 (21)
- .NET (153)
- ROR和GOG (10)
- [网站分类]4.其他技术区 (181)
- 算法等 (7)
- [随笔分类]SOA (8)
- 收藏区 (71)
- 金融证券 (4)
- [网站分类]5.企业信息化 (3)
- c&c++学习 (1)
- 读书区 (11)
- 其它 (10)
- 收藏夹 (1)
- 设计模式 (1)
- FLEX (14)
- Android (98)
- 软件工程心理学系列 (4)
- HTML5 (6)
- C/C++ (0)
- 数据结构 (0)
- 书评 (3)
- python (17)
- NOSQL (10)
- MYSQL (85)
- java之各类测试 (18)
- nodejs (1)
- JAVA (1)
- neo4j (3)
- VUE (4)
- docker相关 (1)
最新评论
-
xiaobadi:
jacky~~~~~~~~~
推荐两个不错的mybatis GUI生成工具 -
masuweng:
(转)JAVA获得机器码的实现 -
albert0707:
有些扩展名为null
java 7中可以判断文件的contenttype了 -
albert0707:
非常感谢!!!!!!!!!
java 7中可以判断文件的contenttype了 -
zhangle:
https://zhuban.me竹板共享 - 高效便捷的文档 ...
一个不错的网络白板工具
java 7中的try catch除了之前谈到的新特性外,本文简单来例子小结下,其实还是有不少地方要注意的,首先看一个典型的代码:
先来两个异常类:
再创建一个资源类oldresource,如下:
我们开始使用之:
看输出:
Doing: Writing an article
Exception Message: Exception occured while doing work Exception Type: javaapplication4.ExceptionA
Closing the resource
Exception Message: Exception occured while closing Exception Type: javaapplication4.ExceptionB
再来看java 7中的新写法,代码如下:
在JAVA 7中,要自动在try catch中利用到新特性,想不写那么多东西就关闭资源,则可以编写实现 AutoCloseable类的,则都可以利用该特性了。
在主程序中调用:
输出结果为:
Listening to podcast
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA
大家可以思考下为什么这样输出,在新特性中,资源的自动关闭调用了close(),而
NewResource res = new NewResource("Res1 closing")){
已经为closingMessage赋值了,而最后的Exception e是输出了,suprred掉了
exception a和exception b的输出。
再看一个多层嵌套的try catch例子
输出:
Wow res getting res to do work
Nestedres closing
Res2 closing
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA
可以看到,后声明的资源上被最先CLOSE掉的,这里各自原先的exception都被supressed掉了。还可以用e.getSuppressed() 把屏蔽掉的exception都放来,比如
输出显示:
Wow res getting res to do work
Nestedres closing
Res2 closing
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA
Exception thrown while closing Class: ExceptionB
Exception thrown while closing Class: ExceptionB
Exception thrown while closing Class: ExceptionB
先来两个异常类:
public class ExceptionA extends Exception{ public ExceptionA(String message){ super(message); } } public class ExceptionB extends Exception{ public ExceptionB(String message){ super(message); } }
再创建一个资源类oldresource,如下:
public class OldResource{ public void doSomeWork(String work) throws ExceptionA{ System.out.println("Doing: "+work); throw new ExceptionA("Exception occured while doing work"); } public void close() throws ExceptionB{ System.out.println("Closing the resource"); throw new ExceptionB("Exception occured while closing"); } }
我们开始使用之:
public class OldTry { public static void main(String[] args) { OldResource res = null; try { res = new OldResource(); res.doSomeWork("Writing an article"); } catch (Exception e) { System.out.println("Exception Message: "+ e.getMessage()+" Exception Type: "+e.getClass().getName()); } finally{ try { res.close(); } catch (Exception e) { System.out.println("Exception Message: "+ e.getMessage()+" Exception Type: "+e.getClass().getName()); } } } }
看输出:
Doing: Writing an article
Exception Message: Exception occured while doing work Exception Type: javaapplication4.ExceptionA
Closing the resource
Exception Message: Exception occured while closing Exception Type: javaapplication4.ExceptionB
再来看java 7中的新写法,代码如下:
public class NewResource implements AutoCloseable{ String closingMessage; public NewResource(String closingMessage) { this.closingMessage = closingMessage; } public void doSomeWork(String work) throws ExceptionA{ System.out.println(work); throw new ExceptionA("Exception thrown while doing some work"); } public void close() throws ExceptionB{ System.out.println(closingMessage); throw new ExceptionB("Exception thrown while closing"); } public void doSomeWork(NewResource res) throws ExceptionA{ res.doSomeWork("Wow res getting res to do work"); } }
在JAVA 7中,要自动在try catch中利用到新特性,想不写那么多东西就关闭资源,则可以编写实现 AutoCloseable类的,则都可以利用该特性了。
在主程序中调用:
public class TryWithRes { public static void main(String[] args) { try(NewResource res = new NewResource("Res1 closing")){ res.doSomeWork("Listening to podcast"); } catch(Exception e){ System.out.println("Exception: "+ e.getMessage()+" Thrown by: "+e.getClass().getSimpleName()); } } }
输出结果为:
Listening to podcast
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA
大家可以思考下为什么这样输出,在新特性中,资源的自动关闭调用了close(),而
NewResource res = new NewResource("Res1 closing")){
已经为closingMessage赋值了,而最后的Exception e是输出了,suprred掉了
exception a和exception b的输出。
再看一个多层嵌套的try catch例子
public class TryWithRes { public static void main(String[] args) { try(NewResource res = new NewResource("Res1 closing"); NewResource res2 = new NewResource("Res2 closing")){ try(NewResource nestedRes = new NewResource("Nestedres closing")){ nestedRes.doSomeWork(res2); } } catch(Exception e){ System.out.println("Exception: "+ e.getMessage()+" Thrown by: "+e.getClass().getSimpleName()); } } }
输出:
Wow res getting res to do work
Nestedres closing
Res2 closing
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA
可以看到,后声明的资源上被最先CLOSE掉的,这里各自原先的exception都被supressed掉了。还可以用e.getSuppressed() 把屏蔽掉的exception都放来,比如
public class TryWithRes { public static void main(String[] args) { try(NewResource res = new NewResource("Res1 closing"); NewResource res2 = new NewResource("Res2 closing")){ try(NewResource nestedRes = new NewResource("Nestedres closing")){ nestedRes.doSomeWork(res2); } } catch(Exception e){ System.out.println("Exception: "+ e.getMessage()+" Thrown by: "+e.getClass().getSimpleName()); if (e.getSuppressed() != null){ for (Throwable t : e.getSuppressed()){ System.out.println(t.getMessage()+ " Class: "+t.getClass().getSimpleName()); } } } } }
输出显示:
Wow res getting res to do work
Nestedres closing
Res2 closing
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA
Exception thrown while closing Class: ExceptionB
Exception thrown while closing Class: ExceptionB
Exception thrown while closing Class: ExceptionB
发表评论
-
复习:强迫线程顺序执行方式
2019-01-03 23:42 1495方法1: 三个线程,t1,t2,t3,如果一定要按顺序执行, ... -
(转)不错的前后端处理异常的方法
2019-01-02 23:16 1972前言 在 Web 开发中, 我们经常会需要处理各种异常, 这是 ... -
info q的极客时间大咖说等资料下载
2018-08-15 08:40 3420info q的极客时间大咖说等资料下载,还有不少思维导图 链 ... -
CXF 客户端超时时间设置(非Spring配置方式)
2018-07-03 22:38 2192import org.apache.cxf.endpoint. ... -
(转)synchronized关键字画像:正确打开方式
2018-06-14 09:25 454https://mp.weixin.qq.com/s/b3Sx ... -
CountDownLatch的例子
2018-06-13 14:10 637public class StatsDemo { ... -
两道面试题,带你解析Java类加载机制
2018-06-12 16:29 563https://mp.weixin.qq.com/s/YTa0 ... -
Spring中获取request的几种方法,及其线程安全性分析
2018-06-11 09:03 631https://mp.weixin.qq.com/s/KeFJ ... -
内部类小结
2018-06-06 10:25 404https://mp.weixin.qq.com/s/hErv ... -
JVM虚拟机小结1
2018-06-04 20:43 4681 jps -l //列出详细的类名和进程ID 2)jps ... -
windows下自带命令行工具查看CPU资源情况等
2018-06-04 12:53 3048微软提供了不少命令行 ... -
(收藏)深入分析Java的序列化与反序列化
2018-05-30 15:21 567https://mp.weixin.qq.com/s/T2Bn ... -
apache common包中的序列化工具
2018-05-30 09:10 1789什么是序列化 我们的 ... -
JAVA8 JVM的变化: 元空间(Metaspace)
2018-05-24 22:30 918本文将会分享至今为至我收集的关于永久代(Permanent G ... -
(转)服务器性能指标(一)——负载(Load)分析及问题排查
2018-05-21 21:03 1272原创: Hollis Hollis 负载 ... -
(转)对象复用
2018-05-20 15:27 815public class Student { priv ... -
mapreduce中入门中要注意的几点
2018-05-06 08:59 626在 mapreduce中,比如有如下的词: I love b ... -
HDFS的基本操作
2018-05-02 21:47 889-mkdir 在HDFS创建目录 ... -
一个不错的开源工具类,专门用来解析日志头部的,好用
2018-05-02 20:00 716一个不错的开源工具类,专门用来解析日志头部的,好用。 http ... -
介绍个不错的RESTFUL MOCK的工具wiremock
2018-04-27 21:02 1860介绍个不错的RESTFUL MOCK的工具wiremock,地 ...
相关推荐
java try…catch嵌套捕获异常的实例 java try…catch嵌套捕获异常的实例
java中try catch的用法 java try catch的用法 java中 try catch的用法
java try…catch捕获异常的实例 java try…catch捕获异常的实例
19.java嵌套的try…catch语句.zip19.java嵌套的try…catch语句.zip19.java嵌套的try…catch语句.zip19.java嵌套的try…catch语句.zip19.java嵌套的try…catch语句.zip19.java嵌套的try…catch语句.zip19.java嵌套的...
17.javatry…catch…finally语句.zip17.javatry…catch…finally语句.zip17.javatry…catch…finally语句.zip17.javatry…catch…finally语句.zip17.javatry…catch…finally语句.zip17.javatry…catch…finally语句...
Java的 try 与 catch finally关键字的使用
压缩包里有本人录制的一个小视屏,不会的亲们,可以看下哦~~~ 笨人要期末考了呢,录些小视屏供自己复习所用,也是为了资源共享~O(∩_∩)O~
try块中包含可能会产生异常的代码,当try块内的代码发生异常时,程序会立即跳转到与异常类型相匹配的catch块进行处理。 在catch块中,你可以编写适当的处理逻辑来处理特定类型的异常。每个catch块可以捕获不同类型...
在Java的异常机制中,如果finally中含有return语句,则try和catch中的return语句将会被JVM忽视
主要介绍了Java中异常处理之try和catch代码块的使用,是Java入门学习中的基础知识,需要的朋友可以参考下
java try…catch…finally捕获异常的实例 java try…catch…finally捕获异常的实例
主要介绍了Java异常处理机制try catch流程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Try…Catch使用小结 Try…Catch使用小结 Try…Catch使用小结
第一:return语句并不是函数的终出口,如果有finally语句,这在return之后还会执行finally(return的值会暂存在栈里面,等待finally执行后再返回) 第二:finally里面不建议放return语句,根据需要,return语句...
主要介绍了Java中try、catch的使用方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
主要介绍了Java try catch finally异常处理组合详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Condition 3: try中有异常,try-catch-finally里都没有return ,finally 之后有个returntry中有异常以后,根据
在本篇文章里小编给大家整理了一篇关于java中throws与try...catch的区别点的内容,需要的朋友们跟着学习下。
javascript 中的try catch应用总结 实例代码: [removed] try { throw new Error(10,"asdasdasd") } catch (e) { alert(e.message); alert(e.description) alert(e.number) alert(e....
C语言异常封装 模仿try catch 源码可以运行,C语言是JAVA的鼻祖