/** * 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); }
/** 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); }
/** * 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; }
/** * @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; }
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; }
/** * 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; } /**
@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); } } }
/** * @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; }
@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; } }
@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; } }
e.initCause(cause); } catch (Throwable t) {
@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; } } }
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; }
@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); } }
@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); } } }
chain.initCause(e); } catch (Throwable t) { // NOPMD
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); }
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); }
@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); } }
@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); }