java.lang.Throwable.addSuppressed()方法的使用及代码示例

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

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

Throwable.addSuppressed介绍

[英]Adds throwable to the list of throwables suppressed by this. The throwable will included when this exception's stack trace is printed.
[中]将throwable添加到受此项抑制的throwable列表中。打印此异常的堆栈跟踪时,将包括throwable。

代码示例

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

private void suppress(Throwable thrown, Throwable suppressed)
 {
  if (thrown != suppressed) {
   thrown.addSuppressed(suppressed);
  }
 }
}

代码示例来源:origin: neo4j/neo4j

public static <T extends Throwable> T chain( T initial, T current )
{
  if ( initial == null )
  {
    return current;
  }
  if ( current != null )
  {
    initial.addSuppressed( current );
  }
  return initial;
}

代码示例来源:origin: prestodb/presto

@SuppressWarnings("ObjectEquality")
  private static void closeWithSuppression(Connection connection, Throwable throwable)
  {
    try {
      connection.close();
    }
    catch (Throwable t) {
      // Self-suppression not permitted
      if (throwable != t) {
        throwable.addSuppressed(t);
      }
    }
  }
}

代码示例来源:origin: com.h2database/h2

private void handleException(Throwable ex) {
  if (backgroundExceptionHandler != null) {
    try {
      backgroundExceptionHandler.uncaughtException(null, ex);
    } catch(Throwable ignore) {
      if (ex != ignore) { // OOME may be the same
        ex.addSuppressed(ignore);
      }
    }
  }
}

代码示例来源:origin: neo4j/neo4j

/**
 * @deprecated Use {@link Throwable#addSuppressed(Throwable)} instead.
 */
@Deprecated
public static <T extends Throwable> T withSuppressed( T exception, Throwable... suppressed )
{
  if ( suppressed != null )
  {
    for ( Throwable s : suppressed )
    {
      exception.addSuppressed( s );
    }
  }
  return exception;
}

代码示例来源:origin: prestodb/presto

private void closeWithSuppression(Throwable throwable)
{
  requireNonNull(throwable, "throwable is null");
  try {
    close();
  }
  catch (Exception e) {
    if (e != throwable) {
      throwable.addSuppressed(e);
    }
  }
}

代码示例来源:origin: prestodb/presto

protected void closeWithSuppression(Throwable throwable)
{
  requireNonNull(throwable, "throwable is null");
  try {
    close();
  }
  catch (RuntimeException e) {
    // Self-suppression not permitted
    if (throwable != e) {
      throwable.addSuppressed(e);
    }
  }
}

代码示例来源:origin: prestodb/presto

private void closeWithSuppression(Throwable throwable)
{
  requireNonNull(throwable, "throwable is null");
  try {
    close();
  }
  catch (RuntimeException e) {
    // Self-suppression not permitted
    if (e != throwable) {
      throwable.addSuppressed(e);
    }
  }
}

代码示例来源:origin: prestodb/presto

protected void closeWithSuppression(Throwable throwable)
{
  requireNonNull(throwable, "throwable is null");
  try {
    close();
  }
  catch (RuntimeException e) {
    // Self-suppression not permitted
    if (throwable != e) {
      throwable.addSuppressed(e);
    }
  }
}

代码示例来源:origin: prestodb/presto

public static void closeWithSuppression(RecordCursor recordCursor, Throwable throwable)
{
  requireNonNull(recordCursor, "recordCursor is null");
  requireNonNull(throwable, "throwable is null");
  try {
    recordCursor.close();
  }
  catch (RuntimeException e) {
    // Self-suppression not permitted
    if (throwable != e) {
      throwable.addSuppressed(e);
    }
  }
}

代码示例来源:origin: netty/netty

@SuppressJava6Requirement(reason = "Throwable addSuppressed is only available for >= 7. Has check for < 7.")
public static void addSuppressed(Throwable target, Throwable suppressed) {
  if (!haveSuppressed()) {
    return;
  }
  target.addSuppressed(suppressed);
}

代码示例来源:origin: redisson/redisson

@SuppressJava6Requirement(reason = "Throwable addSuppressed is only available for >= 7. Has check for < 7.")
public static void addSuppressed(Throwable target, Throwable suppressed) {
  if (!haveSuppressed()) {
    return;
  }
  target.addSuppressed(suppressed);
}

代码示例来源:origin: neo4j/neo4j

private <EX extends Throwable> EX withCallTraces( EX failure )
  {
    for ( StackTraceElement[] waitCall : waitCalls )
    {
      RuntimeException call = new RuntimeException( "Wait called" );
      call.setStackTrace( waitCall );
      failure.addSuppressed( call );
    }
    return failure;
  }
}

代码示例来源:origin: prestodb/presto

public void assertInvalidFunction(String projection, SemanticErrorCode expectedErrorCode)
{
  try {
    evaluateInvalid(projection);
    fail(format("Expected to throw %s exception", expectedErrorCode));
  }
  catch (SemanticException e) {
    try {
      assertEquals(e.getCode(), expectedErrorCode);
    }
    catch (Throwable failure) {
      failure.addSuppressed(e);
      throw failure;
    }
  }
}

代码示例来源:origin: prestodb/presto

public void assertInvalidCast(String projection)
{
  try {
    evaluateInvalid(projection);
    fail("Expected to throw an INVALID_CAST_ARGUMENT exception");
  }
  catch (PrestoException e) {
    try {
      assertEquals(e.getErrorCode(), INVALID_CAST_ARGUMENT.toErrorCode());
    }
    catch (Throwable failure) {
      failure.addSuppressed(e);
      throw failure;
    }
  }
}

代码示例来源:origin: prestodb/presto

public void assertInvalidFunction(String projection, ErrorCodeSupplier expectedErrorCode)
{
  try {
    evaluateInvalid(projection);
    fail(format("Expected to throw %s exception", expectedErrorCode.toErrorCode()));
  }
  catch (PrestoException e) {
    try {
      assertEquals(e.getErrorCode(), expectedErrorCode.toErrorCode());
    }
    catch (Throwable failure) {
      failure.addSuppressed(e);
      throw failure;
    }
  }
}

代码示例来源:origin: prestodb/presto

public void assertNumericOverflow(String projection, String message)
{
  try {
    evaluateInvalid(projection);
    fail("Expected to throw an NUMERIC_VALUE_OUT_OF_RANGE exception with message " + message);
  }
  catch (PrestoException e) {
    try {
      assertEquals(e.getErrorCode(), NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode());
      assertEquals(e.getMessage(), message);
    }
    catch (Throwable failure) {
      failure.addSuppressed(e);
      throw failure;
    }
  }
}

代码示例来源:origin: prestodb/presto

protected void assertNotSupported(String projection, String message)
{
  try {
    functionAssertions.executeProjectionWithFullEngine(projection);
    fail("expected exception");
  }
  catch (PrestoException e) {
    try {
      assertEquals(e.getErrorCode(), NOT_SUPPORTED.toErrorCode());
      assertEquals(e.getMessage(), message);
    }
    catch (Throwable failure) {
      failure.addSuppressed(e);
      throw failure;
    }
  }
}

代码示例来源:origin: prestodb/presto

public void assertInvalidCast(String projection, String message)
{
  try {
    evaluateInvalid(projection);
    fail("Expected to throw an INVALID_CAST_ARGUMENT exception");
  }
  catch (PrestoException e) {
    try {
      assertEquals(e.getErrorCode(), INVALID_CAST_ARGUMENT.toErrorCode());
      assertEquals(e.getMessage(), message);
    }
    catch (Throwable failure) {
      failure.addSuppressed(e);
      throw failure;
    }
  }
}

代码示例来源:origin: prestodb/presto

public void assertInvalidFunction(String projection, StandardErrorCode errorCode, String messagePattern)
{
  try {
    evaluateInvalid(projection);
    fail("Expected to throw a PrestoException with message matching " + messagePattern);
  }
  catch (PrestoException e) {
    try {
      assertEquals(e.getErrorCode(), errorCode.toErrorCode());
      assertTrue(e.getMessage().equals(messagePattern) || e.getMessage().matches(messagePattern), format("Error message [%s] doesn't match [%s]", e.getMessage(), messagePattern));
    }
    catch (Throwable failure) {
      failure.addSuppressed(e);
      throw failure;
    }
  }
}

相关文章