Java Utililty Methods RuntimeException Create

List of utility methods to do RuntimeException Create

Description

The list of methods to do RuntimeException Create are organized into topic(s).

Method

RuntimeExceptiontoRuntimeException(Exception e)
to Runtime Exception
return toRuntimeException(e, null);
RuntimeExceptiontoRuntimeException(Exception e)
Do never catch Throwables!
return e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
RuntimeExceptiontoRuntimeException(Exception ex)
to Runtime Exception
if (ex instanceof RuntimeException) {
    return (RuntimeException) ex;
} else {
    return new RuntimeException(ex);
RuntimeExceptiontoRuntimeException(final Throwable e)
Creates a RuntimeException from the Throwable if it isn't already a RuntimeException
if (e == null) {
    return new NullPointerException("Problem without an Exception");
return e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
RuntimeExceptiontoRuntimeException(Throwable e)
convert Throwable to RuntimeException
RuntimeException re = new RuntimeException(e);
re.setStackTrace(e.getStackTrace());
e.printStackTrace(System.err);
return re;
RuntimeExceptiontoRuntimeException(Throwable t)
Always returns a RuntimeException using this sequential logic:
  1. if t == null, returns a new RuntimeException which has a null cause
  2. else if t is an instanceof Error, returns a new RuntimeException which has t as its cause
  3. else if t is an instanceof RuntimeException, returns t itself
  4. else if t is an instanceof Exception, then it must be a checked Exception, so it returns a new RuntimeException which has t as its cause
  5. else t is an actual Throwable instance (or some unknown subclass), so it returns a new RuntimeException which has t as its cause

This method is usually called to convert checked Exceptions into unchecked ones.

if (t == null)
    return new RuntimeException("This RuntimeException wraps a null cause");
else if (t instanceof Error)
    return new RuntimeException("This RuntimeException wraps an underlying Error (see cause)", t);
else if (t instanceof RuntimeException)
    return (RuntimeException) t;
else if (t instanceof Exception)
    return new RuntimeException("This RuntimeException wraps an underlying checked Exception (see cause)",
...
RuntimeExceptiontoRuntimeException(Throwable t)
to Runtime Exception
return new RuntimeException(t);
RuntimeExceptiontoRuntimeException(Throwable throwable)
to Runtime Exception
return throwable instanceof RuntimeException ? (RuntimeException) throwable
        : new RuntimeException(throwable);
RuntimeExceptiontoRuntimeException(Throwable throwable)
Converts the given throwable into a RuntimeException , if necessary, and returns it.
if (throwable instanceof Error)
    throw (Error) throwable;
return throwable instanceof RuntimeException ? (RuntimeException) throwable
        : new RuntimeException(throwable);
TtoRuntimeExceptionOr(Class exClass, Throwable throwable)
Returns the given throwable if it is of the expected type and returns it, or converts it into a RuntimeException , if necessary, and throws it.
if (exClass.isAssignableFrom(throwable.getClass()))
    return (T) throwable;
if (throwable instanceof RuntimeException)
    throw (RuntimeException) throwable;
if (throwable instanceof Error)
    throw (Error) throwable;
throw new RuntimeException(throwable);