鍍金池/ 教程/ Java/ Guava Throwables類
Guava原語工具
Guava集合工具
Guava Chars類
Guava Shorts類
Guava CharMatcher類
Guava BigIntegerMath類
Guava Range類
Guava Bimap接口
Guava緩存工具
Guava Longs類
Guava Multiset接口
Guava Table接口
Guava Optional類
Guava LongMath類
Guava Spiltter類
Guava Preconditions類
Guava數(shù)學(xué)工具
Guava Ints類
Guava Ordering類
Guava Throwables類
Guava字符串工具
Guava Objects類
Guava Booleans類
Guava教程
Guava Bytes類
Guava CaseFormat類
Guava環(huán)境設(shè)置
Guava Doubles類
Guava Joiner類
Guava Multimap類
Guava Floats類
Guava IntMath類

Guava Throwables類

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

Throwables示例

創(chuàng)建使用所選擇的任何編輯器下面的java程序,比如 C:/> Guava

GuavaTester.java
import 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è)置