Class ExceptionUtil

java.lang.Object
oshi.util.ExceptionUtil

@ThreadSafe public final class ExceptionUtil extends Object
Utility methods for reducing repetitive exception handling boilerplate, particularly around FFM (Foreign Function and Memory) native calls that require catching Throwable.
  • Method Details

    • getOrDefault

      public static <T> T getOrDefault(ExceptionUtil.ThrowingSupplier<T> supplier, T defaultValue)
      Executes the supplier, returning its result on success or the default value if any Throwable is thrown.
      Type Parameters:
      T - the result type
      Parameters:
      supplier - the operation to attempt
      defaultValue - the value to return on failure
      Returns:
      the supplier's result or the default value
    • getOrDefault

      public static <T> T getOrDefault(ExceptionUtil.ThrowingSupplier<T> supplier, T defaultValue, org.slf4j.Logger log, String msg)
      Executes the supplier, returning its result on success or the default value if any Throwable is thrown. Logs the exception at debug level.
      Type Parameters:
      T - the result type
      Parameters:
      supplier - the operation to attempt
      defaultValue - the value to return on failure
      log - the logger to use
      msg - the log message (use {} for the exception message placeholder)
      Returns:
      the supplier's result or the default value
    • getIntOrDefault

      public static int getIntOrDefault(ExceptionUtil.ThrowingIntSupplier supplier, int defaultValue)
      Executes the int supplier, returning its result on success or the default value on failure.
      Parameters:
      supplier - the operation to attempt
      defaultValue - the value to return on failure
      Returns:
      the supplier's result or the default value
    • getIntOrDefault

      public static int getIntOrDefault(ExceptionUtil.ThrowingIntSupplier supplier, int defaultValue, org.slf4j.Logger log, String msg)
      Executes the int supplier, returning its result on success or the default value on failure. Logs the exception at debug level.
      Parameters:
      supplier - the operation to attempt
      defaultValue - the value to return on failure
      log - the logger to use
      msg - the log message
      Returns:
      the supplier's result or the default value
    • getLongOrDefault

      public static long getLongOrDefault(ExceptionUtil.ThrowingLongSupplier supplier, long defaultValue)
      Executes the long supplier, returning its result on success or the default value on failure.
      Parameters:
      supplier - the operation to attempt
      defaultValue - the value to return on failure
      Returns:
      the supplier's result or the default value
    • getLongOrDefault

      public static long getLongOrDefault(ExceptionUtil.ThrowingLongSupplier supplier, long defaultValue, org.slf4j.Logger log, String msg)
      Executes the long supplier, returning its result on success or the default value on failure. Logs the exception at debug level.
      Parameters:
      supplier - the operation to attempt
      defaultValue - the value to return on failure
      log - the logger to use
      msg - the log message
      Returns:
      the supplier's result or the default value
    • getBooleanOrDefault

      public static boolean getBooleanOrDefault(ExceptionUtil.ThrowingBooleanSupplier supplier, boolean defaultValue)
      Executes the boolean supplier, returning its result on success or the default value on failure.
      Parameters:
      supplier - the operation to attempt
      defaultValue - the value to return on failure
      Returns:
      the supplier's result or the default value
    • getBooleanOrDefault

      public static boolean getBooleanOrDefault(ExceptionUtil.ThrowingBooleanSupplier supplier, boolean defaultValue, org.slf4j.Logger log, String msg)
      Executes the boolean supplier, returning its result on success or the default value on failure. Logs the exception at debug level.
      Parameters:
      supplier - the operation to attempt
      defaultValue - the value to return on failure
      log - the logger to use
      msg - the log message
      Returns:
      the supplier's result or the default value
    • getDoubleOrDefault

      public static double getDoubleOrDefault(ExceptionUtil.ThrowingDoubleSupplier supplier, double defaultValue)
      Executes the double supplier, returning its result on success or the default value on failure.
      Parameters:
      supplier - the operation to attempt
      defaultValue - the value to return on failure
      Returns:
      the supplier's result or the default value
    • getDoubleOrDefault

      public static double getDoubleOrDefault(ExceptionUtil.ThrowingDoubleSupplier supplier, double defaultValue, org.slf4j.Logger log, String msg)
      Executes the double supplier, returning its result on success or the default value on failure. Logs the exception at debug level.
      Parameters:
      supplier - the operation to attempt
      defaultValue - the value to return on failure
      log - the logger to use
      msg - the log message
      Returns:
      the supplier's result or the default value
    • getOptional

      public static <T> Optional<T> getOptional(ExceptionUtil.ThrowingSupplier<T> supplier, org.slf4j.Logger log, String msg)
      Executes the supplier, wrapping the result in an Optional. Returns Optional.empty() on failure.
      Type Parameters:
      T - the result type
      Parameters:
      supplier - the operation to attempt
      log - the logger to use
      msg - the log message
      Returns:
      an Optional containing the result, or empty on failure
    • getOptionalInt

      public static OptionalInt getOptionalInt(ExceptionUtil.ThrowingIntSupplier supplier, org.slf4j.Logger log, String msg)
      Executes the int supplier, wrapping the result in an OptionalInt. Returns OptionalInt.empty() on failure.
      Parameters:
      supplier - the operation to attempt
      log - the logger to use
      msg - the log message
      Returns:
      an OptionalInt containing the result, or empty on failure
    • getOptionalLong

      public static OptionalLong getOptionalLong(ExceptionUtil.ThrowingLongSupplier supplier, org.slf4j.Logger log, String msg)
      Executes the long supplier, wrapping the result in an OptionalLong. Returns OptionalLong.empty() on failure.
      Parameters:
      supplier - the operation to attempt
      log - the logger to use
      msg - the log message
      Returns:
      an OptionalLong containing the result, or empty on failure
    • runSilently

      public static void runSilently(ExceptionUtil.ThrowingRunnable runnable)
      Executes the runnable, silently swallowing any Throwable.
      Parameters:
      runnable - the operation to attempt
    • runOrLog

      public static void runOrLog(ExceptionUtil.ThrowingRunnable runnable, org.slf4j.Logger log, String msg)
      Executes the runnable, logging any Throwable at debug level.
      Parameters:
      runnable - the operation to attempt
      log - the logger to use
      msg - the log message