1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-17 23:05:03 -05:00
filebot/source/net/sourceforge/tuned/ExceptionUtilities.java

70 lines
1.2 KiB
Java
Raw Normal View History

package net.sourceforge.tuned;
public final class ExceptionUtilities {
public static Throwable getRootCause(Throwable t) {
while (t.getCause() != null) {
t = t.getCause();
}
return t;
}
public static <T extends Throwable> T findCause(Throwable t, Class<T> type) {
while (t != null) {
if (type.isInstance(t))
2009-05-13 14:20:26 -04:00
return type.cast(t);
t = t.getCause();
}
return null;
}
2009-02-10 16:51:02 -05:00
public static String getRootCauseMessage(Throwable t) {
return getMessage(getRootCause(t));
}
public static String getMessage(Throwable t) {
String message = t.getMessage();
if (message == null || message.isEmpty()) {
message = t.toString();
2009-02-10 16:51:02 -05:00
}
return message;
}
2009-05-12 04:57:37 -04:00
public static <T extends Throwable> T wrap(Throwable t, Class<T> type) {
if (type.isInstance(t)) {
2009-05-13 14:20:26 -04:00
return type.cast(t);
}
2009-05-12 04:57:37 -04:00
try {
return type.getConstructor(Throwable.class).newInstance(t);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
public static RuntimeException asRuntimeException(Throwable t) {
return wrap(t, RuntimeException.class);
}
2008-10-10 15:20:37 -04:00
/**
* Dummy constructor to prevent instantiation.
*/
private ExceptionUtilities() {
2008-10-10 15:20:37 -04:00
throw new UnsupportedOperationException();
}
}