Class ExceptionUtilities
- 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.
-
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).
-