Class ExceptionMethods
java.lang.Object
org.klojang.util.ExceptionMethods
Methods related to exception handling.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringgetDetailedMessage(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 StringgetFilteredStackTrace(Throwable exc, String... filters) Returns the stack trace of the specifiedThrowableas aString, using the specified filters to filter stacktrace elements.static ThrowablegetRootCause(Throwable exc) Returns the root cause of the specifiedThrowable, or theThrowableitself 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 StringReturns the stack trace of the root cause of the specifiedThrowableas aString.static StringgetRootStackTraceAsString(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 StringReturns the stack trace of the specifiedThrowableas aString.static RuntimeExceptionReturns the specified throwable if it already is aRuntimeException, else anRootExceptionexception wrapping the root cause of the provided exception.static RuntimeExceptionReturns the specified throwable if it already is aRuntimeException, else anRootExceptionexception wrapping the root cause of the provided exception.static RuntimeExceptionReturns the specified throwable if it already is aRuntimeException, else anUncheckedExceptionwrapping the throwable.static RuntimeExceptionReturns the specified throwable if it already is aRuntimeException, else anUncheckedExceptionwrapping the throwable.static RuntimeExceptionReturns the specified throwable if it already is aRuntimeException, else aRuntimeExceptionwrapping the throwable.static RuntimeExceptionReturns the specified throwable if it already is aRuntimeException, else aRuntimeExceptionwrapping 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 aRuntimeExceptionproduced by the specified function.static <T extends RuntimeException>
RuntimeExceptionReturns the specified throwable if it already is aRuntimeException, else theRuntimeExceptionreturned by the specified function.
-
Method Details
-
getStackTraceAsString
-
getFilteredStackTrace
Returns the stack trace of the specifiedThrowableas aString, using the specified filters to filter stacktrace elements. The filters will be applied toStackTraceElement.getClassName()using a simpleclassName.contains(filter)operation. The first stacktrace element in the chain of causes is always included in the returned string, irrespective of whether it matches any of the filters.- Parameters:
exc- theThrowablefilters- one or more filters to apply to the stacktrace elements- Returns:
- a filtered stacktrace
-
getRootCause
-
getRootStackTraceAsString
-
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.containson theclassNameproperty 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 nameof the stack trace elementcontainsthe 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 aRuntimeExceptionwrapping the throwable.- Parameters:
exc- a checked or unchecked exception- Returns:
- the specified throwable or a
RuntimeExceptionwrapping it
-
wrap
Returns the specified throwable if it already is aRuntimeException, else aRuntimeExceptionwrapping the throwable.- Parameters:
exc- a checked or unchecked exceptioncustomMessage- a custom message passed on to theRuntimeExceptionwrapping the original exceptionmsgArgs- theString.formatmessage arguments to the custom message- Returns:
- the specified throwable or a
RuntimeExceptionwrapping 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 theRuntimeExceptionreturned 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 aRuntimeExceptionexceptionFactory- a function that converts the specified throwable into aRuntimeException, typically a method reference (e.g.IOException::new)- Returns:
- the specified throwable or a
RuntimeExceptionwrapping 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 aRuntimeExceptionproduced 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 aRuntimeExceptionexceptionFactory- the producer of theRuntimeException, typically the two-argument constructor of anExceptionthat takes aStringargument and aThrowableargumentcustomMessage- a custom message passed on to theRuntimeExceptionwrapping the original exceptionmsgArgs- theString.formatmessage arguments to the custom message- Returns:
- the specified throwable or a
RuntimeExceptionwrapping it
-
uncheck
Returns the specified throwable if it already is aRuntimeException, else anUncheckedExceptionwrapping 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 anIOExceptionwhich is documented as being thrown "if an I/O error occurs".- Parameters:
exc- a checked or unchecked exception- Returns:
- the provided
Throwableor anUncheckedExceptionwrapping it - See Also:
-
uncheck
Returns the specified throwable if it already is aRuntimeException, else anUncheckedExceptionwrapping 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 anIOExceptionwhich 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
Throwableor anUncheckedExceptionwrapping it - See Also:
-
rootCause
Returns the specified throwable if it already is aRuntimeException, else anRootExceptionexception wrapping the root cause of the provided exception.- Parameters:
exc- a checked or unchecked exception- Returns:
- the provided
Throwableor anUncheckedExceptionwrapping it - See Also:
-
rootCause
Returns the specified throwable if it already is aRuntimeException, else anRootExceptionexception 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
Throwableor aRootExceptionwrapping its root cause - See Also:
-