java.lang.Object
org.klojang.util.ExceptionMethods
Methods related to exception handling.
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
getDetailedMessage
(Throwable exc, String search) Provides a detailed exception message that includes the class, method and line number of the first statement in the specified exception's stack trace that matches the search string.static Throwable
getRootCause
(Throwable exc) Returns the root cause of the specifiedThrowable
, or theThrowable
itself if it has no cause.static StackTraceElement[]
getRootStackTrace
(Throwable exc, String... filter) Returns the stack trace of the root cause of the specified exception, using the specified string(s) to filter stack trace elements.static String
Returns the stack trace of the root cause of the specifiedThrowable
as aString
.static String
getRootStackTraceAsString
(Throwable exc, String... filter) Returns the exception message and stack trace of the root cause of the specifiedThrowable
, using the specified string(s) to filter stack trace elements.static RuntimeException
Returns the specified throwable if it already is aRuntimeException
, else anRootException
exception wrapping the root cause of the provided exception.static RuntimeException
Returns the specified throwable if it already is aRuntimeException
, else anRootException
exception wrapping the root cause of the provided exception.static RuntimeException
Returns the specified throwable if it already is aRuntimeException
, else anUncheckedException
wrapping the throwable.static RuntimeException
Returns the specified throwable if it already is aRuntimeException
, else anUncheckedException
wrapping the throwable.static RuntimeException
Returns the specified throwable if it already is aRuntimeException
, else aRuntimeException
wrapping the throwable.static RuntimeException
Returns the specified throwable if it already is aRuntimeException
, else aRuntimeException
wrapping the throwable.static <T extends RuntimeException>
RuntimeExceptionwrap
(Throwable exception, BiFunction<String, Throwable, T> exceptionFactory, String customMessage, Object... msgArgs) Returns the specified throwable if it already is aRuntimeException
, else aRuntimeException
produced by the specified function.static <T extends RuntimeException>
RuntimeExceptionReturns the specified throwable if it already is aRuntimeException
, else theRuntimeException
returned by the specified function.
-
Method Details
-
getRootCause
Returns the root cause of the specifiedThrowable
, or theThrowable
itself if it has no cause.- Parameters:
exc
- the exception whose root cause to retrieve- Returns:
- the root cause of the exception
-
getRootStackTraceAsString
Returns the stack trace of the root cause of the specifiedThrowable
as aString
.- Parameters:
exc
- the exception- Returns:
- the root stack trace as a string
-
getRootStackTraceAsString
Returns the exception message and stack trace of the root cause of the specifiedThrowable
, using the specified string(s) to filter stack trace elements. The stack trace is filtered using a simpleString.contains
on theclassName
property of theStackTraceElement
.- Parameters:
exc
- the exceptionfilter
- one or more filters on stack trace elements- Returns:
- the root stack trace as a string
-
getRootStackTrace
Returns the stack trace of the root cause of the specified exception, using the specified string(s) to filter stack trace elements. If theclass name
of the stack trace elementcontains
the filter string, the stack trace element will be included in the returned array.- Parameters:
exc
- the exceptionfilter
- One or more filters on stack trace elements- Returns:
- the root stack trace
-
getDetailedMessage
Provides a detailed exception message that includes the class, method and line number of the first statement in the specified exception's stack trace that matches the search string. Note that this may not be the absolute origin of the exception - the statement from which the exception was thrown. If the search term does happen to hit upon the absolute origin of the exception, the detailed exception message will include a notification to that effect. Otherwise the class, method and line number of the statement that did throw the exception is also included in the detailed exception message.try { // stuff ... } catch (Exception e) { // Log exception message plus class and line number within the // com.mycompany code base where things flew off the rails logger.error(new ExceptionOrigin(e, "com.mycompany").getDetailedMessage()); }
- Parameters:
exc
- the exception to extract the extra information fromsearch
- The (partial) name of the package or class you want to zoom in on- Returns:
- A detailed exception message
- See Also:
-
wrap
Returns the specified throwable if it already is aRuntimeException
, else aRuntimeException
wrapping the throwable.- Parameters:
exc
- a checked or unchecked exception- Returns:
- the specified throwable or a
RuntimeException
wrapping it
-
wrap
Returns the specified throwable if it already is aRuntimeException
, else aRuntimeException
wrapping the throwable.- Parameters:
exc
- a checked or unchecked exceptioncustomMessage
- a custom message passed on to theRuntimeException
wrapping the original exceptionmsgArgs
- theString.format
message arguments to the custom message- Returns:
- the specified throwable or a
RuntimeException
wrapping it
-
wrap
public static <T extends RuntimeException> RuntimeException wrap(Throwable exc, Function<Throwable, T> exceptionFactory) Returns the specified throwable if it already is aRuntimeException
, else theRuntimeException
returned by the specified function. The function wis given the original exception (exc
) as argument.- Type Parameters:
T
- the type of theRuntimeException
- Parameters:
exc
- the exception to be wrapped if it is not aRuntimeException
exceptionFactory
- a function that converts the specified throwable into aRuntimeException
, typically a method reference (e.g.IOException::new
)- Returns:
- the specified throwable or a
RuntimeException
wrapping it
-
wrap
public static <T extends RuntimeException> RuntimeException wrap(Throwable exception, BiFunction<String, Throwable, T> exceptionFactory, String customMessage, Object... msgArgs) Returns the specified throwable if it already is aRuntimeException
, else aRuntimeException
produced by the specified function. For example:try { // stuff ... } catch(Throwable t) { throw ExceptionMethods.wrap(t, "Bad stuff happening", IllegalStateException::new); }
- Type Parameters:
T
- The type of theRuntimeException
- Parameters:
exception
- the exception to be wrapped if it is not aRuntimeException
exceptionFactory
- the producer of theRuntimeException
, typically the two-argument constructor of anException
that takes aString
argument and aThrowable
argumentcustomMessage
- a custom message passed on to theRuntimeException
wrapping the original exceptionmsgArgs
- theString.format
message arguments to the custom message- Returns:
- the specified throwable or a
RuntimeException
wrapping it
-
uncheck
Returns the specified throwable if it already is aRuntimeException
, else anUncheckedException
wrapping the throwable. This method is primarily meant to "uncheck" checked exceptions that you cannot in practice properly deal with, and are therefore, for all practical purposes, a runtime exception. For example anIOException
which is documented as being thrown "if an I/O error occurs".- Parameters:
exc
- a checked or unchecked exception- Returns:
- the provided
Throwable
or anUncheckedException
wrapping it - See Also:
-
uncheck
Returns the specified throwable if it already is aRuntimeException
, else anUncheckedException
wrapping the throwable. This method is primarily meant to "uncheck" checked exceptions that you cannot in practice properly deal with, and are therefore, for all practical purposes, a runtime exception. For example anIOException
which is documented as being thrown "if an I/O error occurs".- Parameters:
exc
- a checked or unchecked exceptioncustomMessage
- a custom message to pass to the constructor ofUncheckedException
- Returns:
- the provided
Throwable
or anUncheckedException
wrapping it - See Also:
-
rootCause
Returns the specified throwable if it already is aRuntimeException
, else anRootException
exception wrapping the root cause of the provided exception.- Parameters:
exc
- a checked or unchecked exception- Returns:
- the provided
Throwable
or anUncheckedException
wrapping it - See Also:
-
rootCause
Returns the specified throwable if it already is aRuntimeException
, else anRootException
exception wrapping the root cause of the provided exception.- Parameters:
exc
- a checked or unchecked exceptioncustomMessage
- a custom message to pass to the constructor ofUncheckedException
- Returns:
- the provided
Throwable
or aRootException
wrapping its root cause - See Also:
-