Class ExceptionUtilities

java.lang.Object
com.cedarsoftware.util.ExceptionUtilities

public final class ExceptionUtilities extends Object
Useful Exception Utilities
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 Details

    • getDeepestException

      public static Throwable getDeepestException(Throwable e)
      Returns:
      Throwable representing the actual cause (most nested exception).
    • safelyIgnoreException

      public static <T> T safelyIgnoreException(Callable<T> callable, T defaultValue)
      Executes the provided Callable and returns its result. If the callable throws any Throwable, the method returns the specified defaultValue instead.

      Warning: This method suppresses all Throwable instances, including Errors and RuntimeExceptions. 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 - the Callable to execute
      defaultValue - the default value to return if the callable throws an exception
      Returns:
      the result of callable.call() if no exception is thrown, otherwise defaultValue
      Throws:
      IllegalArgumentException - if callable is null
      See Also:
    • safelyIgnoreException

      public static void safelyIgnoreException(Runnable runnable)
      Executes the provided Runnable and safely ignores any exceptions thrown during its execution.

      Warning: This method suppresses all Throwable instances, including Errors and RuntimeExceptions. Use this method with caution, as it can make debugging difficult by hiding critical errors.

      Parameters:
      runnable - the Runnable to execute
    • safelyIgnoreException

      public static void safelyIgnoreException(Throwable t)
      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).