简化异常处理的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方法 ...
随机推荐
- The 1st tip of DB Query Analyzer
The 1st tip of DB Query Analyzer Ma Genfeng (Guangdong Unitoll Services incorporate ...
- C# 如何合并Excel工作表
文档合并.拆分是实现文档管理的一种有效方式.在工作中,我们可能会遇到需要将多个文档合并的情况,那如何来实现呢,本文将进一步介绍.关于拆分Excel工作表,可参见这篇文章--C#如何拆分EXCEL工作表 ...
- centos下 redmind2.6安装
1.下载安装redmind有关软件 cd /tmp wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.5.tar.gz wget http:/ ...
- java 远程调试方法
http://wenku.baidu.com/link?url=5p3GZhPcfvM-VOzAFeCjbLeVv0OQrAGJh4HxirqImuK9VxPfmW243T_l5Plj6KdDZB1I ...
- Unknown entity: org.jbpm.services.task.audit.TaskEventImpl
1. use this persistence.xml - simply copy it into src/main/resources/META-INF please note the name o ...
- 安装 Anaconda 的正确姿势
下面以 Anaconda2 安装为例, 说明如何更加流畅的使用 Conda Install Anaconda2 安装 Anaconda2(从清华源下载比较快) wget https://mirrors ...
- Emit方式调用方法
object objRet = Delegate.CreateDelegate(typeof(Func<Guid, int, decimal>), inst, "HelloWor ...
- iframe中 父页面和子页面查找元素的方法
从父页面中查找iframe子页面中对象的方法:JS: document.getElementById('iframe').contentWindow //查找iframe加载的页面的window对象 ...
- SAE提供服务分析
这个分析列表主要关注两个问题,服务能做什么,移植实现难度. AppConfig: 这个东西主要面向SAE本身的一些配置选项,移植时放弃这个东西,所以就不谈难度了Counter :这个东西提供某个操作的 ...
- log4j2.xml全配置文件
可以参考如下配置 <?xml version="1.0" encoding="UTF-8"?> <!--日志级别以及优先级排序: OFF &g ...