简化异常处理的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方法 ...
随机推荐
- asp.net core上使用redis探索(1)
基于Ubuntu安装redis, 我找的一个很好的网站: https://www.digitalocean.com/community/tutorials/how-to-install-and-con ...
- DB2 SQL Error: SQLCODE=-803, SQLSTATE=23505, SQLERRMC=2 (转载)
http://blog.csdn.net/xiyuan1999/article/details/5706230 DB2 SQL Error: SQLCODE=-803, SQLSTATE=23505, ...
- TensorFlow学习记录(一)
windows下的安装: 首先访问https://storage.googleapis.com/tensorflow/ 找到对应操作系统下,对应python版本,对应python位数的whl,下载. ...
- Storyboard中ViewController加载的四种方式
这个总结来自于<Programming iOS 10>一书: 1.storyboard的初始化ViewController,通过方法instantiateInitialViewContro ...
- WebApi PUT、DELETE请求时出现405 - 不允许用于访问此页的 HTTP 谓词。
开发时,新建WebApi项目需要用到Restful规范,此时请求有POST\PUT\DELETE\GET等请求 此时需要在web.config中加入 <system.webServer> ...
- .Net中stirng转Systen.Type的一种实现思路
今天在上班的过程中,许长时间未联系的大学小伙伴发来消息,带着一个疑问来找我. 他的需求是type动态添加,这对我来说当然很easy,用泛型就好了, 随后,手起刀落,Demo就写出来,如下: 写了一个方 ...
- C#本质论笔记
第一章 C#概述 1.1 Helo,World 学习一种新语言最好的办法就是动手写程序. C#编译器创建的.exe程序是一个程序集(Assembly),我们也可以创建能由另一个较大的程序 ...
- node传统读取文件和promise,async await,
先上传统文件加载方式代码,传统方式在处理多层嵌套时代码比较混乱 const fs = require('fs') //引入文件系统 function readFile (cb) { fs.readFi ...
- NoHttp封装--03 缓存
1.Default模式,也是没有设置缓存模式时的默认模式 这个模式实现http协议中的内容,比如响应码是304时,当然还会结合E-Tag和LastModify等头. StringRequest req ...
- DDD实战进阶第一波(十):开发一般业务的大健康行业直销系统(实现经销商登录仓储与逻辑)
上一篇文章主要讲了经销商注册的仓储和领域逻辑的实现,我们先把应用服务协调完成经销商注册这部分暂停一下,后面文章统一讲. 这篇文章主要讲讲经销商登录的仓储和相关逻辑的实现. 在现代应用程序前后端分离的实 ...