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 Throwablestatic voidsafelyIgnoreException(Runnable runnable) Executes the providedRunnableand safely ignores any exceptions thrown during its execution.static voidSafely Ignore a Throwable or rethrow if it is a Throwable that should not be ignored.static <T> TsafelyIgnoreException(Callable<T> callable, T defaultValue) Executes the providedCallableand returns its result.
-
Method Details
-
getDeepestException
- Returns:
- Throwable representing the actual cause (most nested exception).
-
safelyIgnoreException
Executes the providedCallableand returns its result. If the callable throws anyThrowable, the method returns the specifieddefaultValueinstead.Warning: This method suppresses all
Throwableinstances, includingErrors andRuntimeExceptions. 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 thrownWhen 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
Throwableinstances.- Type Parameters:
T- the type of the result returned by the callable- Parameters:
callable- theCallableto 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- ifcallableisnull- See Also:
-
safelyIgnoreException
Executes the providedRunnableand safely ignores any exceptions thrown during its execution.Warning: This method suppresses all
Throwableinstances, includingErrors andRuntimeExceptions. Use this method with caution, as it can make debugging difficult by hiding critical errors.- Parameters:
runnable- theRunnableto 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).
-