com.fasterxml.jackson.databind.util.ExceptionUtil Maven / Gradle / Ivy
The newest version!
package com.fasterxml.jackson.databind.util;
import java.io.IOException;
/**
* Utility methods for dealing with exceptions/throwables
*
* @since 2.15
*/
public class ExceptionUtil {
private ExceptionUtil() {}
/**
* It is important never to catch all Throwable
s. Some like
* {@link InterruptedException} should be rethrown. Based on
* scala.util.control.NonFatal.
*
* This method should be used with care.
*
* If the Throwable
is fatal, it is rethrown, otherwise, this method just returns.
* The input throwable is thrown if it is an Error
or a RuntimeException
.
* Otherwise, the method wraps the throwable in a RuntimeException and throws that.
*
*
* @param throwable to check
* @throws Error the input throwable if it is fatal
* @throws RuntimeException the input throwable if it is fatal - throws the original throwable
* if is a RuntimeException
. Otherwise, wraps the throwable in a RuntimeException.
*/
public static void rethrowIfFatal(Throwable throwable) throws Error, RuntimeException {
if (isFatal(throwable)) {
if (throwable instanceof Error) {
throw (Error) throwable;
}
if (throwable instanceof RuntimeException) {
throw (RuntimeException) throwable;
}
throw new RuntimeException(throwable);
}
}
/**
* It is important never to catch all Throwable
s. Some like
* {@link InterruptedException} should be rethrown. Based on
* scala.util.control.NonFatal.
*
* @param throwable to check
* @return whether the Throwable
is a fatal error
*/
private static boolean isFatal(Throwable throwable) {
return (throwable instanceof VirtualMachineError
|| throwable instanceof ThreadDeath
|| throwable instanceof InterruptedException
|| throwable instanceof ClassCircularityError
|| throwable instanceof ClassFormatError
|| throwable instanceof IncompatibleClassChangeError
|| throwable instanceof BootstrapMethodError
|| throwable instanceof VerifyError
);
}
public static T throwSneaky(IOException e) {
_sneaky(e);
return null; // never gets here, needed for compiler tho
}
@SuppressWarnings("unchecked")
private static void _sneaky(Throwable e) throws E {
throw (E) e;
}
}