简化异常处理的Throwables类
简化异常处理的Throwables类
有时候, 当我们我们捕获异常, 并且像把这个异常传递到下一个try/catch块中。Guava提供了一个异常处理工具类, 可以简单地捕获和重新抛出多个异常。例如:
import java.io.IOException;
import org.junit.Test;
import com.google.common.base.Throwables;
public class ThrowablesTest {
@Test
public void testThrowables(){
try {
throw new Exception();
} catch (Throwable t) {
String ss = Throwables.getStackTraceAsString(t);
System.out.println("ss:"+ss);
Throwables.propagate(t);
}
}
@Test
public void call() throws IOException {
try {
throw new IOException();
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, IOException.class);
throw Throwables.propagate(t);
}
}
}
将检查异常转换成未检查异常,例如:
import java.io.InputStream;
import java.net.URL;
import org.junit.Test;
import com.google.common.base.Throwables;
public class ThrowablesTest {
@Test
public void testCheckException(){
try {
URL url = new URL("http://ociweb.com");
final InputStream in = url.openStream();
// read from the input stream
in.close();
} catch (Throwable t) {
throw Throwables.propagate(t);
}
}
}
传递异常的常用方法:
1.RuntimeException propagate(Throwable):把throwable包装成RuntimeException,用该方法保证异常传递,抛出一个RuntimeException异常
2.void propagateIfInstanceOf(Throwable, Class<X extends Exception>) throws X:当且仅当它是一个X的实例时,传递throwable
3.void propagateIfPossible(Throwable):当且仅当它是一个RuntimeException和Error时,传递throwable
4.void propagateIfPossible(Throwable, Class<X extends Throwable>) throws X:当且仅当它是一个RuntimeException和Error时,或者是一个X的实例时,传递throwable。
使用实例:
import java.io.IOException;
import org.junit.Test;
import com.google.common.base.Throwables;
public class ThrowablesTest {
@Test
public void testThrowables(){
try {
throw new Exception();
} catch (Throwable t) {
String ss = Throwables.getStackTraceAsString(t);
System.out.println("ss:"+ss);
Throwables.propagate(t);
}
}
@Test
public void call() throws IOException {
try {
throw new IOException();
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, IOException.class);
throw Throwables.propagate(t);
}
}
public Void testPropagateIfPossible() throws Exception {
try {
throw new Exception();
} catch (Throwable t) {
Throwables.propagateIfPossible(t, Exception.class);
Throwables.propagate(t);
}
return null;
}
}
Guava的异常链处理方法:
1.Throwable getRootCause(Throwable)
2.List<Throwable> getCausalChain(Throwable)
3.String getStackTraceAsString(Throwable)
有时候, 当我们我们捕获异常, 并且像把这个异常传递到下一个try/catch块中。Guava提供了一个异常处理工具类, 可以简单地捕获和重新抛出多个异常。例如:

import java.io.IOException;
import org.junit.Test;
import com.google.common.base.Throwables; public class ThrowablesTest { @Test
public void testThrowables(){
try {
throw new Exception();
} catch (Throwable t) {
String ss = Throwables.getStackTraceAsString(t);
System.out.println("ss:"+ss);
Throwables.propagate(t);
}
} @Test
public void call() throws IOException {
try {
throw new IOException();
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, IOException.class);
throw Throwables.propagate(t);
}
}
}

将检查异常转换成未检查异常,例如:

import java.io.InputStream;
import java.net.URL;
import org.junit.Test;
import com.google.common.base.Throwables; public class ThrowablesTest { @Test
public void testCheckException(){
try {
URL url = new URL("http://ociweb.com");
final InputStream in = url.openStream();
// read from the input stream
in.close();
} catch (Throwable t) {
throw Throwables.propagate(t);
}
}
}

传递异常的常用方法:
1.RuntimeException propagate(Throwable):把throwable包装成RuntimeException,用该方法保证异常传递,抛出一个RuntimeException异常
2.void propagateIfInstanceOf(Throwable, Class<X extends Exception>) throws X:当且仅当它是一个X的实例时,传递throwable
3.void propagateIfPossible(Throwable):当且仅当它是一个RuntimeException和Error时,传递throwable
4.void propagateIfPossible(Throwable, Class<X extends Throwable>) throws X:当且仅当它是一个RuntimeException和Error时,或者是一个X的实例时,传递throwable。
使用实例:

import java.io.IOException;
import org.junit.Test;
import com.google.common.base.Throwables; public class ThrowablesTest {
@Test
public void testThrowables(){
try {
throw new Exception();
} catch (Throwable t) {
String ss = Throwables.getStackTraceAsString(t);
System.out.println("ss:"+ss);
Throwables.propagate(t);
}
} @Test
public void call() throws IOException {
try {
throw new IOException();
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, IOException.class);
throw Throwables.propagate(t);
}
} public Void testPropagateIfPossible() throws Exception {
try {
throw new Exception();
} catch (Throwable t) {
Throwables.propagateIfPossible(t, Exception.class);
Throwables.propagate(t);
} return null;
}
}

Guava的异常链处理方法:
1.Throwable getRootCause(Throwable)
2.List<Throwable> getCausalChain(Throwable)
3.String getStackTraceAsString(Throwable)
简化异常处理的Throwables类的更多相关文章
- Guava学习笔记:简化异常处理的Throwables类
有时候, 当我们我们捕获异常, 并且像把这个异常传递到下一个try/catch块中.Guava提供了一个异常处理工具类, 可以简单地捕获和重新抛出多个异常.例如: import java.io.IOE ...
- JAVA异常处理、常用类、反射、集合
异常 异常:在Java中是指被一个方法抛出的对象. 分类:检查异常.运行时异常.错误 运行时异常(uncheckd):RuntimeException和其子类 检查异常(checkd/搜检异常):指E ...
- java异常,异常处理,异常类 关键字:throws 和 throw 自定义的异常类
package cn.kecheng; import java.util.Scanner; /**异常:异常是指在程序的运行过程中所发生的不正常的情况,它会中断正在运行的程序 异常处理机制:java中 ...
- python的异常处理及异常类定义
python的异常处理语法和大多数语言相似: try: try块的语句... except exceptiontype1 as var:#使用as语句获得本次捕获到的异常的实例var except块语 ...
- Python异常处理及元类
一.异常处理 异常是错误发生的信号,一旦程序出错就会产生一个异常,如果该异常没有被应用程序处理,那么该异常就会跑出来,程序的执行也随之终止,也就是说异常就是一个事件,该事件会在程序执行过程中发生,影响 ...
- Java学习--异常处理及其应用类
异常是运行时在代码序列中引起的非正常状况,换句话说,异常是运行时错误.在不支持异常处理的计算机语言中,必须手动检查和处理错误----通常是通过使用错误代码,等等.这种方式既笨拙又麻烦.Java的异常处 ...
- Python 29 异常处理, 元类
所学内容 异常处理(常用) AttributeError ·························· 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性xIOError ··· ...
- 课堂动手动脑验证以及自定义异常类实现对异常处理——java异常类
异常(exception):发生在程序执行期间,表明出现了一个非法运行的情况.许多JDK中的方法在检测到非法情况时,都会抛出一个异常对象.例如:数组越界和被0除. 代码验证: package test ...
- spring boot 全局异常处理及自定义异常类
全局异常处理: 在处理controller层抛出的自定义异常时,可以实现@ControllerAdvice注解捕获,配合@ExceptionHandler来增强所有的@requestMapping方法 ...
随机推荐
- 抛开rails使用ActiveRecord连接数据库
今天是大年三十,明天就正式进入羊年鸟,给所有程序猿(媛)同人拜个年吧!祝大家身体健康,事业有成,财源广进哦! 话归正题,以前都是在rails中使用数据库,或者在rails的console中使用:我们如 ...
- DB2常用命令2
1.启动实例(db2inst1):实例相当于informix中的服务 db2start 2.停止实例(db2inst1): db2stop 3.列出所有实例(db2inst1) db2ilist 4. ...
- pslq常用操作
1,登录后默认自动选中My Objects 默认情况下,PLSQL Developer登录后,Brower里会选择All objects,如果你登录的用户是dba,要展开tables目录,正常情况都 ...
- Ibatis和Hibernate的比较
Ibatis和Hibernate的比较 分类: IBATIS HIBERNATE2010-11-19 17:58 341人阅读 评论(0) 收藏 举报 hibernateibatis数据库sqlcac ...
- git push 报错 "Peer certificate cannot be authenticated with known CA certificates"
使用git push -u origin master 命令向远程仓库提交代码时报错:Peer certificate cannot be authenticated with known CA ce ...
- git merge 与 git rebase
git merge git rebase merge V.S. rebase 参考材料 写在开始: 对merge和rebase的用法总有疑惑,好像两个都能完成"获取别的branch的comm ...
- 解决ubuntu unity下gvim菜单消失的问题
#问题描述:在终端下用gvim 指令打开 gvim就不显示菜单.在不启用unity的桌面环境下用终端打开gvim是有菜单的.从程序菜单中打开gvim是显示菜单的.用sudo打开gvim也可以显示菜单, ...
- JavaScript路线
看到知乎上有大神回答的,感觉很不错,分享下 首先要说明的是,咱现在不是高手,最多还是一个半桶水,算是入了JS的门. 谈不上经验,都是一些教训. 这个时候有人要说,“靠,你丫半桶水,凭啥教我们”.您先别 ...
- 在Java中谈尾递归--尾递归和垃圾回收的比较(转载)
我不是故意在JAVA中谈尾递归的,因为在JAVA中谈尾递归真的是要绕好几个弯,只是我确实只有JAVA学得比较好,虽然确实C是在学校学过还考了90+,真学得没自学的JAVA好 不过也是因为要绕几个弯,所 ...
- RabbitMQ Linux(Redhat6.5)安装(二 )
一.安装erlang 由于RabbitMq的linux运行环境需要erlang环境,所以需要先安装erlang: 1.erlang下载: http://erlang.org/download/(我下载 ...