Class ExceptionUtilities
uncheckedThrow(Throwable)
helper which allows rethrowing any
Throwable
without declaring it.- Author:
- Ken Partlow ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Throwable
static void
safelyIgnoreException
(Runnable runnable) Executes the providedRunnable
and safely ignores any exceptions thrown during its execution.static void
Safely Ignore a Throwable or rethrow if it is a Throwable that should not be ignored.static <T> T
safelyIgnoreException
(Callable<T> callable, T defaultValue) Executes the providedCallable
and returns its result.static <T extends Throwable>
voidThrows anyThrowable
without declaring it.
-
Method Details
-
getDeepestException
- Returns:
- Throwable representing the actual cause (most nested exception).
-
safelyIgnoreException
Executes the providedCallable
and returns its result. If the callable throws anyThrowable
, the method returns the specifieddefaultValue
instead.Warning: This method suppresses all
Throwable
instances, includingError
s andRuntimeException
s. Use this method with caution, as it can make debugging difficult by hiding critical errors.Usage Example:
// Example using safelyIgnoreException with a Callable that may throw an exception String result = safelyIgnoreException(() -> potentiallyFailingOperation(), "defaultValue"); System.out.println(result); // Outputs the result of the operation or "defaultValue" if an exception was thrown
When to Use: Use this method in scenarios where you want to execute a task that might throw an exception, but you prefer to provide a fallback value instead of handling the exception explicitly. This can simplify code in cases where exception handling is either unnecessary or handled elsewhere.
Caution: Suppressing all exceptions can obscure underlying issues, making it harder to identify and fix problems. It is generally recommended to handle specific exceptions that you expect and can recover from, rather than catching all
Throwable
instances.- Type Parameters:
T
- the type of the result returned by the callable- Parameters:
callable
- theCallable
to executedefaultValue
- the default value to return if the callable throws an exception- Returns:
- the result of
callable.call()
if no exception is thrown, otherwisedefaultValue
- Throws:
IllegalArgumentException
- ifcallable
isnull
- See Also:
-
safelyIgnoreException
Executes the providedRunnable
and safely ignores any exceptions thrown during its execution.Warning: This method suppresses all
Throwable
instances, includingError
s andRuntimeException
s. Use this method with caution, as it can make debugging difficult by hiding critical errors.- Parameters:
runnable
- theRunnable
to execute
-
safelyIgnoreException
Safely Ignore a Throwable or rethrow if it is a Throwable that should not be ignored.- Parameters:
t
- Throwable to possibly ignore (ThreadDeath and OutOfMemory are not ignored).
-
uncheckedThrow
Throws anyThrowable
without declaring it. Useful when converting Groovy code to Java or otherwise bypassing checked exceptions. No longer do you need to declare checked exceptions, which are not always best handled by the immediate calling class. This will still an IOException, for example, without you declaring as a throws clause forcing the caller to deal with it, where as a higher level more suitable place that catches Exception will still catch it as an IOException (in this case). Helps the shift away from Checked exceptions, which imho, was not a good choice for the Java language.- Type Parameters:
T
- type parameter used to trick the compiler- Parameters:
t
- throwable to be rethrown unchecked- Throws:
T
- never actually thrown, but declared for compiler satisfaction
-