java.lang.Throwable类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.6k)|赞(0)|评价(0)|浏览(238)

本文整理了Java中java.lang.Throwable类的一些代码示例,展示了Throwable类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Throwable类的具体详情如下:
包路径:java.lang.Throwable
类名称:Throwable

Throwable介绍

[英]The superclass of all classes which can be thrown by the VM. The two direct subclasses are recoverable exceptions ( Exception) and unrecoverable errors ( Error). This class provides common methods for accessing a string message which provides extra information about the circumstances in which the Throwable was created (basically an error message in most cases), and for saving a stack trace (that is, a record of the call stack at a particular point in time) which can be printed later.

A Throwable can also include a cause, which is a nested Throwable that represents the original problem that led to this Throwable. It is often used for wrapping various types of errors into a common Throwable without losing the detailed original error information. When printing the stack trace, the trace of the cause is included.
[中]VM可以抛出的所有类的超类。两个直接子类是可恢复异常(Exception)和不可恢复错误(Error)。此类提供了访问字符串消息的常用方法,该消息提供了有关创建Throwable的环境的额外信息(在大多数情况下基本上是错误消息),并提供了保存堆栈跟踪(即特定时间点的调用堆栈记录)的常用方法,该跟踪可在以后打印。
一次性垃圾还可以包括一个原因,这是一个嵌套的一次性垃圾,表示导致此一次性垃圾的原始问题。它通常用于将各种类型的错误包装到一个通用的一次性文件中,而不会丢失详细的原始错误信息。打印堆栈跟踪时,会包括原因跟踪。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Constructor that takes a plain root cause, intended for
 * subclasses mirroring corresponding {@code javax.jms} exceptions.
 * @param cause the cause of the exception. This argument is generally
 * expected to be a proper subclass of {@link javax.jms.JMSException}.
 */
public JmsException(@Nullable Throwable cause) {
  super(cause != null ? cause.getMessage() : null, cause);
}

代码示例来源:origin: square/okhttp

@Override public void onFailure(WebSocket webSocket, Throwable t, Response response) {
  t.printStackTrace(System.out);
  latch.countDown();
 }
});

代码示例来源:origin: ReactiveX/RxJava

static void assertUndeliverableTestException(List<Throwable> list, int index, String message) {
  assertTrue(list.get(index).toString(), list.get(index).getCause() instanceof TestException);
  assertEquals(message, list.get(index).getCause().getMessage());
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean matches(Method method, Class<?> targetClass, Object... args) {
  this.evaluations++;
  for (StackTraceElement element : new Throwable().getStackTrace()) {
    if (element.getClassName().equals(this.clazz.getName()) &&
        (this.methodName == null || element.getMethodName().equals(this.methodName))) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: ReactiveX/RxJava

private void appendStackTrace(StringBuilder b, Throwable ex, String prefix) {
  b.append(prefix).append(ex).append('\n');
  for (StackTraceElement stackElement : ex.getStackTrace()) {
    b.append("\t\tat ").append(stackElement).append('\n');
  }
  if (ex.getCause() != null) {
    b.append("\tCaused by: ");
    appendStackTrace(b, ex.getCause(), "");
  }
}

代码示例来源:origin: google/guava

/**
  * Appends the chain of messages from the {@code conflictingStackTrace} to the original {@code
  * message}.
  */
 @Override
 public String getMessage() {
  StringBuilder message = new StringBuilder(super.getMessage());
  for (Throwable t = conflictingStackTrace; t != null; t = t.getCause()) {
   message.append(", ").append(t.getMessage());
  }
  return message.toString();
 }
}

代码示例来源:origin: ReactiveX/RxJava

static void assertTestException(List<Throwable> list, int index, String message) {
  assertTrue(list.get(index).toString(), list.get(index) instanceof TestException);
  assertEquals(message, list.get(index).getMessage());
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public void onError(Throwable e) {
  System.out.println("error: " + e.getMessage());
  e.printStackTrace();
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Check "javax.servlet.error.exception" attribute for a multipart exception.
 */
private boolean hasMultipartException(HttpServletRequest request) {
  Throwable error = (Throwable) request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE);
  while (error != null) {
    if (error instanceof MultipartException) {
      return true;
    }
    error = error.getCause();
  }
  return false;
}

代码示例来源:origin: ReactiveX/RxJava

public static void assertError(TestObserver<?> to, int index, Class<? extends Throwable> clazz) {
  Throwable ex = to.errors().get(0);
  try {
    if (ex instanceof CompositeException) {
      CompositeException ce = (CompositeException) ex;
      List<Throwable> cel = ce.getExceptions();
      assertTrue(cel.get(index).toString(), clazz.isInstance(cel.get(index)));
    } else {
      fail(ex.toString() + ": not a CompositeException");
    }
  } catch (AssertionError e) {
    ex.printStackTrace();
    throw e;
  }
}

代码示例来源:origin: square/okhttp

/**
 * Returns an object that holds a stack trace created at the moment this method is executed. This
 * should be used specifically for {@link java.io.Closeable} objects and in conjunction with
 * {@link #logCloseableLeak(String, Object)}.
 */
public Object getStackTraceForCloseable(String closer) {
 if (logger.isLoggable(Level.FINE)) {
  return new Throwable(closer); // These are expensive to allocate.
 }
 return null;
}

代码示例来源:origin: spring-projects/spring-framework

@Override
  public String toString() {
    return getCause().toString();
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
  public void writeObject(Object obj, AbstractHessianOutput out)
      throws IOException {
    Throwable e = (Throwable) obj;

    e.getStackTrace();

    super.writeObject(obj, out);
  }
}

代码示例来源:origin: ReactiveX/RxJava

public static void assertError(TestObserver<?> to, int index, Class<? extends Throwable> clazz, String message) {
  Throwable ex = to.errors().get(0);
  if (ex instanceof CompositeException) {
    CompositeException ce = (CompositeException) ex;
    List<Throwable> cel = ce.getExceptions();
    assertTrue(cel.get(index).toString(), clazz.isInstance(cel.get(index)));
    assertEquals(message, cel.get(index).getMessage());
  } else {
    fail(ex.toString() + ": not a CompositeException");
  }
}

代码示例来源:origin: ReactiveX/RxJava

private static Throwable getRootCause(Throwable ex) {
  Throwable root = ex.getCause();
  if (root == null) {
    return null;
  } else {
    while (true) {
      if (root.getCause() == null) {
        return root;
      } else {
        root = root.getCause();
      }
    }
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void subscribe(Subscriber<? super String> subscriber) {
    subscribed.set(true);
    subscriber.onSubscribe(bs);
    subscriber.onError(new Throwable("test failed"));
  }
});

代码示例来源:origin: ReactiveX/RxJava

static void assertNPE(List<Throwable> list, int index) {
  assertTrue(list.get(index).toString(), list.get(index) instanceof NullPointerException);
}

代码示例来源:origin: spring-projects/spring-framework

private boolean contain(Throwable t, String className, String methodName) {
  for (StackTraceElement element : t.getStackTrace()) {
    if (className.equals(element.getClassName()) && methodName.equals(element.getMethodName())) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: google/guava

@Override
public final void tearDown() {
 try {
  sloppyTearDown();
 } catch (Throwable t) {
  logger.log(Level.INFO, "exception thrown during tearDown: " + t.getMessage(), t);
 }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void accept(Throwable t1) {
    t1.printStackTrace();
  }
},

相关文章