ExceptionUtil

Introduction

Exception encapsulation, such as wrapping as RuntimeException.

Methods

Wrapping Exceptions

Assuming the system throws a non-Runtime exception that needs to be wrapped as a Runtime exception, we can use:

IORuntimeException e = ExceptionUtil.wrap(new IOException(), IORuntimeException.class);

Getting the Entry Method

StackTraceElement ele = ExceptionUtil.getRootStackElement();
// main
ele.getMethodName();

Exception Conversion

If we want to convert an exception to a specified exception or include a specified exception, we can use:

IOException ioException = new IOException();
IllegalArgumentException argumentException = new IllegalArgumentException(ioException);

IOException ioException1 = ExceptionUtil.convertFromOrSuppressedThrowable(argumentException, IOException.class, true);

Other Methods

  • getMessage to get the complete message, including the exception name.
  • wrapRuntime to wrap a checked exception with a runtime exception.
  • getCausedBy to get the exception caused by a specified exception class.
  • isCausedBy to determine if it is caused by a specified exception class.
  • stacktraceToString to convert a stack trace to a complete string.

For other methods, refer to the API documentation:

https://apidoc.gitee.com/dromara/hutool/cn/hutool/core/exceptions/ExceptionUtil.html