Class ExceptionUtil
java.lang.Object
oshi.util.ExceptionUtil
Utility methods for reducing repetitive exception handling boilerplate, particularly around FFM (Foreign Function and
Memory) native calls that require catching
Throwable.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA boolean-returning supplier that may throw anyThrowable.static interfaceA double-returning supplier that may throw anyThrowable.static interfaceAn int-returning supplier that may throw anyThrowable.static interfaceA long-returning supplier that may throw anyThrowable.static interfaceA runnable that may throw anyThrowable.static interfaceA supplier that may throw anyThrowable, including checked exceptions. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleangetBooleanOrDefault(ExceptionUtil.ThrowingBooleanSupplier supplier, boolean defaultValue) Executes the boolean supplier, returning its result on success or the default value on failure.static booleangetBooleanOrDefault(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.static doublegetDoubleOrDefault(ExceptionUtil.ThrowingDoubleSupplier supplier, double defaultValue) Executes the double supplier, returning its result on success or the default value on failure.static doublegetDoubleOrDefault(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.static intgetIntOrDefault(ExceptionUtil.ThrowingIntSupplier supplier, int defaultValue) Executes the int supplier, returning its result on success or the default value on failure.static intgetIntOrDefault(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.static longgetLongOrDefault(ExceptionUtil.ThrowingLongSupplier supplier, long defaultValue) Executes the long supplier, returning its result on success or the default value on failure.static longgetLongOrDefault(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.static <T> Optional<T> getOptional(ExceptionUtil.ThrowingSupplier<T> supplier, org.slf4j.Logger log, String msg) Executes the supplier, wrapping the result in anOptional.static OptionalIntgetOptionalInt(ExceptionUtil.ThrowingIntSupplier supplier, org.slf4j.Logger log, String msg) Executes the int supplier, wrapping the result in anOptionalInt.static OptionalLonggetOptionalLong(ExceptionUtil.ThrowingLongSupplier supplier, org.slf4j.Logger log, String msg) Executes the long supplier, wrapping the result in anOptionalLong.static <T> TgetOrDefault(ExceptionUtil.ThrowingSupplier<T> supplier, T defaultValue) Executes the supplier, returning its result on success or the default value if anyThrowableis thrown.static <T> TgetOrDefault(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 anyThrowableis thrown.static voidrunOrLog(ExceptionUtil.ThrowingRunnable runnable, org.slf4j.Logger log, String msg) Executes the runnable, logging anyThrowableat debug level.static voidrunSilently(ExceptionUtil.ThrowingRunnable runnable) Executes the runnable, silently swallowing anyThrowable.
-
Method Details
-
getOrDefault
Executes the supplier, returning its result on success or the default value if anyThrowableis thrown.- Type Parameters:
T- the result type- Parameters:
supplier- the operation to attemptdefaultValue- 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 anyThrowableis thrown. Logs the exception at debug level.- Type Parameters:
T- the result type- Parameters:
supplier- the operation to attemptdefaultValue- the value to return on failurelog- the logger to usemsg- the log message (use {} for the exception message placeholder)- Returns:
- the supplier's result or the default value
-
getIntOrDefault
Executes the int supplier, returning its result on success or the default value on failure.- Parameters:
supplier- the operation to attemptdefaultValue- 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 attemptdefaultValue- the value to return on failurelog- the logger to usemsg- the log message- Returns:
- the supplier's result or the default value
-
getLongOrDefault
Executes the long supplier, returning its result on success or the default value on failure.- Parameters:
supplier- the operation to attemptdefaultValue- 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 attemptdefaultValue- the value to return on failurelog- the logger to usemsg- 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 attemptdefaultValue- 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 attemptdefaultValue- the value to return on failurelog- the logger to usemsg- 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 attemptdefaultValue- 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 attemptdefaultValue- the value to return on failurelog- the logger to usemsg- 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 anOptional. ReturnsOptional.empty()on failure.- Type Parameters:
T- the result type- Parameters:
supplier- the operation to attemptlog- the logger to usemsg- 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 anOptionalInt. ReturnsOptionalInt.empty()on failure.- Parameters:
supplier- the operation to attemptlog- the logger to usemsg- 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 anOptionalLong. ReturnsOptionalLong.empty()on failure.- Parameters:
supplier- the operation to attemptlog- the logger to usemsg- the log message- Returns:
- an OptionalLong containing the result, or empty on failure
-
runSilently
Executes the runnable, silently swallowing anyThrowable.- Parameters:
runnable- the operation to attempt
-
runOrLog
public static void runOrLog(ExceptionUtil.ThrowingRunnable runnable, org.slf4j.Logger log, String msg) Executes the runnable, logging anyThrowableat debug level.- Parameters:
runnable- the operation to attemptlog- the logger to usemsg- the log message
-