教程 > guava 教程 > 阅读:27

guava throwables 类——迹忆客-ag捕鱼王app官网

throwables 类提供与 throwable 接口相关的实用方法。


类声明

以下是 com.google.common.base.throwables 类的声明

public final class throwables
   extends object

类方法

序号 方法 说明
1 static list getcausalchain(throwable throwable) 获取一个 throwable 原因链作为列表。
2 static throwable getrootcause(throwable throwable) 返回 throwable 的最内在原因。
3 static string getstacktraceasstring(throwable throwable) 返回包含 tostring() 结果的字符串,后跟 throwable 的完整递归堆栈跟踪。
4 static runtimeexception propagate(throwable throwable) 如果它是 runtimeexception 或 error 的实例,则按原样传播 throwable,否则作为最后的手段,将其包装在 runtimeexception 中然后传播。
5 static void propagateifinstanceof(throwable throwable, class declaredtype) 完全按原样传播 throwable,当且仅当它是 declaredtype 的实例时。
6 static void propagateifpossible(throwable throwable) 完全按原样传播 throwable,当且仅当它是 runtimeexception 或 error 的实例时。
7 static void propagateifpossible(throwable throwable, class declaredtype) 完全按原样传播 throwable,当且仅当它是 runtimeexception、error 或 declaredtype 的实例时。
8 static void propagateifpossible(throwable throwable, class declaredtype1, class declaredtype2) 完全按原样传播 throwable,当且仅当它是 runtimeexception、error、declaredtype1 或 declaredtype2 的实例时。

方法继承

该类继承了以下类的方法 -

  • java.lang.object

throwables 类示例

c:/> guava 中使用我们选择的任何编辑器创建以下 java 程序。

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 new invalidinputexception();
      return math.sqrt(input);
   }
   public double getvalue(int[] list, int index) throws indexoutofboundsexception {
      return list[index];
   }
   public void dummyio() throws ioexception {
      throw new ioexception();
   }
}
class invalidinputexception extends exception {
}

验证结果

使用 javac 编译器编译类,如下所示

c:\guava>javac guavatester.java

现在运行 guavatester 以查看结果。

c:\guava>java guavatester

结果如下

invalidinputexception
java.lang.arrayindexoutofboundsexception: 4
   at guavatester.getvalue(guavatester.java:52)
   at guavatester.showcasethrowables1(guavatester.java:38)
   at guavatester.main(guavatester.java:19)

查看笔记

扫码一下
查看教程更方便
网站地图