Guava学习笔记【5】:简化异常处理的Throwables类
来源:互联网 发布:太原理工软件学院几本 编辑:程序博客网 时间:2024/06/05 19:03
有时候, 当我们我们捕获异常, 并且像把这个异常传递到下一个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)
0 0
- Guava学习笔记【5】:简化异常处理的Throwables类
- Guava学习笔记:简化异常处理的Throwables类
- [Guava]Throwables:简化异常和错误的传播与检查
- Google Guava学习(5)-Guava Throwables类
- [Google Guava] 1.5-Throwables:简化异常和错误的传播与检查
- [Google Guava] 1.5-Throwables:简化异常和错误的传播与检查
- [Google Guava] 1.5-Throwables:简化异常和错误的传播与检查
- Java-类库-Guava-Throwables类
- Throwables帮你抛出异常,处理异常
- guava学习之——集合类创建的简化。
- Guava Optional类的使用【Guava学习笔记】
- Guava Objects类的使用方式【Guava学习笔记】
- Guava Preconditions类使用【Guava学习笔记】
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- Guava学习笔记:Guava cache
- 转:ceph技能树
- Codeforces Round #221 (Div. 2)-C. Divisible by Seven
- tomcat与http协议 笔记
- ReactNative入门教程第一步 初始化环境 将HelloWorld运行到手机或者模拟器上
- Guava学习笔记【4】:复写的Object常用方法
- Guava学习笔记【5】:简化异常处理的Throwables类
- Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
- Guava学习笔记【6】:Immutable(不可变)集合
- 原生js利用ajax、FormData对象实现进度条上传
- Guava学习笔记【7】:Guava新增集合类型-Multiset
- AndroidStudio&&Eclipse配置SVN
- BZOJ 1478 Sgu282 Isomorphism
- 简单的js onChange()
- Guava学习笔记【8】:Guava新增集合类型-Multimap