private TrimmingException(Throwable t, int toTrim) { // Trim the trace StackTraceElement[] trace = t.getStackTrace(); StackTraceElement[] trimmed = new StackTraceElement[trace.length - toTrim]; System.arraycopy(trace, 0, trimmed, 0, trimmed.length); setStackTrace(trimmed); // this.toString = t.toString(); this.source = t; // Recursively build the causes Throwable cause = t.getCause(); if (cause != null) { initCause(new TrimmingException(cause, toTrim)); } }
@Test public void testEmpty() { try { TrimmingException.invoke(new TrimmingException.Callback() { public void call() throws Exception { throw new UnsupportedOperationException(); } }); } catch (TrimmingException e) { assertEquals(0, e.getStackTrace().length); assertEquals(null, e.getCause()); } }
public static void invoke(Callback callback) throws TrimmingException { try { callback.call(); } catch (Throwable t) { throw new TrimmingException(t); } }
@Test public void testEmpty() { try { TrimmingException.invoke(new TrimmingException.Callback() { public void call() throws Exception { throw new UnsupportedOperationException(); } }); } catch (TrimmingException e) { assertEquals(0, e.getStackTrace().length); assertEquals(null, e.getCause()); } }
public static void invoke(Callback callback) throws TrimmingException { try { callback.call(); } catch (Throwable t) { throw new TrimmingException(t); } }
@Test public void testException() { try { TrimmingException.invoke(new TrimmingException.Callback() { public void call() throws Exception { throw bar(); } }); } catch (TrimmingException e) { assertEquals(1, e.getStackTrace().length); StackTraceElement elt = e.getStackTrace()[0]; assertEquals(getClass().getName(), elt.getClassName()); assertEquals("bar", elt.getMethodName()); assertEquals(null, e.getCause()); } }
private TrimmingException(Throwable t, int toTrim) { // Trim the trace StackTraceElement[] trace = t.getStackTrace(); StackTraceElement[] trimmed = new StackTraceElement[trace.length - toTrim]; System.arraycopy(trace, 0, trimmed, 0, trimmed.length); setStackTrace(trimmed); // this.toString = t.toString(); this.source = t; // Recursively build the causes Throwable cause = t.getCause(); if (cause != null) { initCause(new TrimmingException(cause, toTrim)); } }
@Test public void testException() { try { TrimmingException.invoke(new TrimmingException.Callback() { public void call() throws Exception { throw bar(); } }); } catch (TrimmingException e) { assertEquals(1, e.getStackTrace().length); StackTraceElement elt = e.getStackTrace()[0]; assertEquals(getClass().getName(), elt.getClassName()); assertEquals("bar", elt.getMethodName()); assertEquals(null, e.getCause()); } }
@Test public void testCause() { try { TrimmingException.invoke(new TrimmingException.Callback() { public void call() throws Exception { throw foo(); } }); } catch (TrimmingException e) { assertEquals(1, e.getStackTrace().length); StackTraceElement elt = e.getStackTrace()[0]; assertEquals(getClass().getName(), elt.getClassName()); assertEquals("foo", elt.getMethodName()); Throwable cause = e.getCause(); assertEquals(2, cause.getStackTrace().length); elt = cause.getStackTrace()[0]; assertEquals(getClass().getName(), elt.getClassName()); assertEquals("bar", elt.getMethodName()); elt = cause.getStackTrace()[1]; assertEquals(getClass().getName(), elt.getClassName()); assertEquals("foo", elt.getMethodName()); assertEquals(null, cause.getCause()); } }
@Test public void testCause() { try { TrimmingException.invoke(new TrimmingException.Callback() { public void call() throws Exception { throw foo(); } }); } catch (TrimmingException e) { assertEquals(1, e.getStackTrace().length); StackTraceElement elt = e.getStackTrace()[0]; assertEquals(getClass().getName(), elt.getClassName()); assertEquals("foo", elt.getMethodName()); Throwable cause = e.getCause(); assertEquals(2, cause.getStackTrace().length); elt = cause.getStackTrace()[0]; assertEquals(getClass().getName(), elt.getClassName()); assertEquals("bar", elt.getMethodName()); elt = cause.getStackTrace()[1]; assertEquals(getClass().getName(), elt.getClassName()); assertEquals("foo", elt.getMethodName()); assertEquals(null, cause.getCause()); } }