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

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

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

Throwable.initCause介绍

[英]Initializes the cause of this Throwable. The cause can only be initialized once.
[中]初始化此丢弃的原因。原因只能初始化一次。

代码示例

代码示例来源:origin: facebook/litho

/**
 * If you are chaining multiple Runnable together that are going to schedule each other within
 * their run() method, use this constructor to track their stacktraces across threads. The
 * required parameter here is the argument coming from {@link #tracedRun(Throwable)}.
 */
public ThreadTracingRunnable(Throwable prevTracingThrowable) {
 this();
 mTracingThrowable.initCause(prevTracingThrowable);
}

代码示例来源:origin: org.netbeans.api/org-openide-filesystems

/** Copies anotation.
 * @param newEx new exception to annotate
 * @param oldEx old exception to take annotation from
 * @return newEx
 */
public static Throwable copyAnnotation(Throwable newEx, Throwable oldEx) {
  return newEx.initCause(oldEx);
}

代码示例来源:origin: aNNiMON/Lightweight-Stream-API

/**
 * Returns inner value if there were no exceptions, otherwise throws the given {@code exception}.
 *
 * @param <E> the type of exception
 * @param exception  an exception to be thrown
 * @return inner value if there were no exceptions
 * @throws E if there were exceptions in supplier function
 */
public <E extends Throwable> T getOrThrow(E exception) throws E {
  if (throwable != null) {
    exception.initCause(throwable);
    throw exception;
  }
  return value;
}

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

/**
 * @deprecated Use {@link Throwable#initCause(Throwable)} instead.
 */
@Deprecated
public static <T extends Throwable> T withCause( T exception, Throwable cause )
{
  try
  {
    exception.initCause( cause );
  }
  catch ( Exception failure )
  {
    // OK, we did our best, guess there will be no cause
  }
  return exception;
}

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

private static <T extends Throwable> T copyCause(T newThrowable, Throwable originalThrowable) {
  Throwable cause = originalThrowable.getCause();
  if (cause != null) try {
    newThrowable.initCause(cause);
  } catch (IllegalStateException ignored) {
    // some exceptions rudely don't allow cause initialization
  }
  return newThrowable;
}

代码示例来源:origin: apache/ignite

/**
 * Utility method that sets cause into exception and returns it.
 *
 * @param e Exception to set cause to and return.
 * @param cause Optional cause to set (if not {@code null}).
 * @param <E> Type of the exception.
 * @return Passed in exception with optionally set cause.
 */
public static <E extends Throwable> E withCause(E e, @Nullable Throwable cause) {
  assert e != null;
  if (cause != null)
    e.initCause(cause);
  return e;
}
/**

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

@Override public void onError(Throwable throwable) {
  if (!terminated) {
   observer.onError(throwable);
  } else {
   // This should never happen! onNext handles and forwards errors automatically.
   Throwable broken = new AssertionError(
     "This should never happen! Report as a bug with the full stacktrace.");
   //noinspection UnnecessaryInitCause Two-arg AssertionError constructor is 1.7+ only.
   broken.initCause(throwable);
   RxJavaPlugins.onError(broken);
  }
 }
}

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

/**
 * @deprecated Use {@link Throwable#addSuppressed(Throwable)} and {@link Throwable#initCause(Throwable)} where
 * appropriate instead.
 */
@Deprecated
public static <E extends Throwable> E combine( E first, E second )
{
  if ( first == null )
  {
    return second;
  }
  if ( second == null )
  {
    return first;
  }
  Throwable current = first;
  while ( current.getCause() != null )
  {
    current = current.getCause();
  }
  current.initCause( second );
  return first;
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

@SuppressWarnings("unchecked")
private static <T extends IOException> T wrapWithMessage(
  T exception, String msg) throws T {
 Class<? extends Throwable> clazz = exception.getClass();
 try {
  Constructor<? extends Throwable> ctor = clazz.getConstructor(String.class);
  Throwable t = ctor.newInstance(msg);
  return (T)(t.initCause(exception));
 } catch (Throwable e) {
  LOG.warn("Unable to wrap exception of type {}: it has no (String) "
    + "constructor", clazz, e);
  throw exception;
 }
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

@SuppressWarnings("unchecked")
private static <T extends IOException> T wrapWithMessage(
  final T exception, final String msg) throws T {
 Class<? extends Throwable> clazz = exception.getClass();
 try {
  Constructor<? extends Throwable> ctor = clazz
    .getConstructor(String.class);
  Throwable t = ctor.newInstance(msg);
  return (T) (t.initCause(exception));
 } catch (Throwable e) {
  LOG.warn("Unable to wrap exception of type " +
    clazz + ": it has no (String) constructor", e);
  throw exception;
 }
}

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

e.initCause(cause);
} catch (Throwable t) {

代码示例来源:origin: facebook/litho

@Override
 public final void run() {
  try {
   tracedRun(mTracingThrowable);
  } catch (Throwable t) {
   if (ComponentsConfiguration.enableThreadTracingStacktrace) {
    Throwable lastThrowable = t;
    while (lastThrowable.getCause() != null) {
     lastThrowable = lastThrowable.getCause();
    }
    lastThrowable.initCause(mTracingThrowable);
   }

   throw t;
  }
 }
}

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

private static TraceInformation getOrAddTraceInformation(Throwable t) {
  if (t == null) {
    throw new NullPointerException("t is null");
  }
  Throwable c;
  while (! (t instanceof TraceInformation)) {
    c = t.getCause();
    if (c == null) try {
      t.initCause(c = new TraceInformation());
    } catch (RuntimeException e) {
      // ignored
    }
    t = c;
  }
  return (TraceInformation) t;
}

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

@Override public void onError(Throwable throwable) {
 if (!subscriberTerminated) {
  subscriber.onError(throwable);
 } else {
  // This should never happen! onNext handles and forwards errors automatically.
  Throwable broken = new AssertionError(
    "This should never happen! Report as a Retrofit bug with the full stacktrace.");
  //noinspection UnnecessaryInitCause Two-arg AssertionError constructor is 1.7+ only.
  broken.initCause(throwable);
  RxJavaPlugins.getInstance().getErrorHandler().handleError(broken);
 }
}

代码示例来源:origin: jeasonlzy/okhttp-OkGo

@Override
  public void onError(Throwable throwable) {
    if (!terminated) {
      observer.onError(throwable);
    } else {
      // This should never happen! onNext handles and forwards errors automatically.
      Throwable broken = new AssertionError("This should never happen! Report as a bug with the full stacktrace.");
      //noinspection UnnecessaryInitCause Two-arg AssertionError constructor is 1.7+ only.
      broken.initCause(throwable);
      RxJavaPlugins.onError(broken);
    }
  }
}

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

chain.initCause(e);
} catch (Throwable t) { // NOPMD

代码示例来源:origin: org.netbeans.api/org-openide-util

private static AnnException findOrCreate0(Throwable t, boolean create) {
  if (t instanceof AnnException) {
    return (AnnException) t;
  }
  if (t.getCause() == null) {
    if (create) {
      t.initCause(new AnnException());
    }
    return (AnnException) t.getCause();
  }
  return findOrCreate0(t.getCause(), create);
}

代码示例来源:origin: com.thoughtworks.xstream/xstream

public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
  Throwable throwable = (Throwable) source;
  if (throwable.getCause() == null) {
    try {
      throwable.initCause(null);
    } catch (IllegalStateException e) {
      // ignore, initCause failed, cause was already set
    }
  }
  throwable.getStackTrace(); // Force stackTrace field to be lazy loaded by special JVM native witchcraft (outside our control).
  getConverter().marshal(throwable, writer, context);
}

代码示例来源:origin: jeasonlzy/okhttp-OkGo

@Override
public void onError(Throwable throwable) {
  if (!subscriberTerminated) {
    subscriber.onError(throwable);
  } else {
    Throwable broken = new AssertionError("This should never happen! Report as a bug with the full stacktrace.");
    //noinspection UnnecessaryInitCause Two-arg AssertionError constructor is 1.7+ only.
    broken.initCause(throwable);
    RxJavaHooks.getOnError().call(broken);
  }
}

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

@Test
public void complexCauses() {
  Throwable e1 = new Throwable("1");
  Throwable e2 = new Throwable("2");
  e1.initCause(e2);
  Throwable e3 = new Throwable("3");
  Throwable e4 = new Throwable("4");
  e3.initCause(e4);
  Throwable e5 = new Throwable("5");
  Throwable e6 = new Throwable("6");
  e5.initCause(e6);
  CompositeException compositeException = new CompositeException(e1, e3, e5);
  assertTrue(compositeException.getCause() instanceof CompositeExceptionCausalChain);
  List<Throwable> causeChain = new ArrayList<Throwable>();
  Throwable cause = compositeException.getCause().getCause();
  while (cause != null) {
    causeChain.add(cause);
    cause = cause.getCause();
  }
  // The original relations
  //
  // e1 -> e2
  // e3 -> e4
  // e5 -> e6
  //
  // will be set to
  //
  // e1 -> e2 -> e3 -> e4 -> e5 -> e6
  assertEquals(Arrays.asList(e1, e2, e3, e4, e5, e6), causeChain);
}

相关文章