Throwable類提供了相關(guān)的Throwable接口的實(shí)用方法。
以下是com.google.common.base.Throwables類的聲明:
public final class Throwables extends Object
S.N. | 方法及說明 |
---|---|
1 |
static List<Throwable> getCausalChain(Throwable throwable) 獲取一個Throwable的原因鏈的列表。 |
2 |
static Throwable getRootCause(Throwable throwable) 返回拋出的最里面的原因。 |
3 |
static String getStackTraceAsString(Throwable throwable) 返回包含toString()的結(jié)果字符串,隨后完整拋出,遞歸的堆棧跟蹤。 |
4 |
static RuntimeException propagate(Throwable throwable) 傳播拋出原樣如果RuntimeException或Error是一個實(shí)例,否則作為最后的報(bào)告,把它包裝在一個RuntimeException,然后傳播。 |
5 |
static <X extends Throwable> void propagateIfInstanceOf(Throwable throwable, Class<X> declaredType) 傳播拋出對象完全按原樣,當(dāng)且僅當(dāng)它是declaredType的一個實(shí)例。 |
6 |
static void propagateIfPossible(Throwable throwable) 傳播拋出對象完全按原樣,當(dāng)且僅當(dāng)它是RuntimeException或Error的一個實(shí)例。 |
7 |
static <X extends Throwable> void propagateIfPossible(Throwable throwable, Class<X> declaredType) 傳播拋出對象完全按原樣,當(dāng)且僅當(dāng)它是RuntimeException,錯誤或的declaredType的一個實(shí)例。 |
8 |
static <X1 extends Throwable,X2 extends Throwable>void propagateIfPossible(Throwable throwable, Class<X1> declaredType1, Class<X2> declaredType2) 傳播拋出對象完全按原樣,當(dāng)且僅當(dāng)它是RuntimeException,Error,declaredType1或declaredType2的一個實(shí)例。 |
這個類繼承了以下類方法:
java.lang.Object
創(chuàng)建使用所選擇的任何編輯器下面的java程序,比如 C:/> Guava
GuavaTester.javaimport java.io.IOException; import com.google.common.base.Objects; import com.google.common.base.Throwables; public class GuavaTester { public static void main(String args[]){ GuavaTester tester = new GuavaTester(); try { tester.showcaseThrowables(); } catch (InvalidInputException e) { //get the root cause System.out.println(Throwables.getRootCause(e)); }catch (Exception e) { //get the stack trace in string format System.out.println(Throwables.getStackTraceAsString(e)); } try { tester.showcaseThrowables1(); }catch (Exception e) { System.out.println(Throwables.getStackTraceAsString(e)); } } public void showcaseThrowables() throws InvalidInputException{ try { sqrt(-3.0); } catch (Throwable e) { //check the type of exception and throw it Throwables.propagateIfInstanceOf(e, InvalidInputException.class); Throwables.propagate(e); } } public void showcaseThrowables1(){ try { int[] data = {1,2,3}; getValue(data, 4); } catch (Throwable e) { Throwables.propagateIfInstanceOf(e, IndexOutOfBoundsException.class); Throwables.propagate(e); } } public double sqrt(double input) throws InvalidInputException{ if(input < 0) throw上一篇:Guava緩存工具下一篇:Guava環(huán)境設(shè)置