diff --git a/src/main/java/rx/exceptions/CompositeException.java b/src/main/java/rx/exceptions/CompositeException.java index 79a49a7e74..d251cf2e95 100644 --- a/src/main/java/rx/exceptions/CompositeException.java +++ b/src/main/java/rx/exceptions/CompositeException.java @@ -118,12 +118,13 @@ public synchronized Throwable getCause() { // we now have 'e' as the last in the chain try { chain.initCause(e); + chain = chain.getCause(); } catch (Throwable t) { // ignore // the javadocs say that some Throwables (depending on how they're made) will never // let me call initCause without blowing up even if it returns null + chain = e; } - chain = chain.getCause(); } cause = _cause; } diff --git a/src/test/java/rx/exceptions/CompositeExceptionTest.java b/src/test/java/rx/exceptions/CompositeExceptionTest.java index 5fadadd42c..fc28e5b21b 100644 --- a/src/test/java/rx/exceptions/CompositeExceptionTest.java +++ b/src/test/java/rx/exceptions/CompositeExceptionTest.java @@ -178,4 +178,46 @@ public void testNullElement() { composite.getCause(); composite.printStackTrace(); } + + @Test(timeout = 1000) + public void testCompositeExceptionWithUnsupportedInitCause() { + Throwable t = new Throwable() { + @Override + public synchronized Throwable initCause(Throwable cause) { + throw new UnsupportedOperationException(); + } + }; + CompositeException cex = new CompositeException(Arrays.asList(t, ex1)); + + System.err.println("----------------------------- print composite stacktrace"); + cex.printStackTrace(); + assertEquals(2, cex.getExceptions().size()); + + assertNoCircularReferences(cex); + assertNotNull(getRootCause(cex)); + + System.err.println("----------------------------- print cause stacktrace"); + cex.getCause().printStackTrace(); + } + + @Test(timeout = 1000) + public void testCompositeExceptionWithNullInitCause() { + Throwable t = new Throwable("ThrowableWithNullInitCause") { + @Override + public synchronized Throwable initCause(Throwable cause) { + return null; + } + }; + CompositeException cex = new CompositeException(Arrays.asList(t, ex1)); + + System.err.println("----------------------------- print composite stacktrace"); + cex.printStackTrace(); + assertEquals(2, cex.getExceptions().size()); + + assertNoCircularReferences(cex); + assertNotNull(getRootCause(cex)); + + System.err.println("----------------------------- print cause stacktrace"); + cex.getCause().printStackTrace(); + } } \ No newline at end of file