Throwable (Throwable ex) { boolean foreignException = false; Throwable each = ex; while (each != null) { if (!each.getClass().getName().startsWith("java.") && !isKnownException(each)) { foreignException = true; break; } each = each.getCause(); } if (foreignException) { ForeignException wrapper = ForeignException.create(ex.toString(), ex.getClass()); wrapper.initCause(wrapException(ex.getCause())); wrapper.setStackTrace(ex.getStackTrace()); ex = wrapper; } return ex; }