chinabugotech/hutool · error · RuntimeException

{}

Error message

{}

What it means

ExceptionUtil.wrapRuntimeAndThrow(message) is a tiny helper that does exactly: throw new RuntimeException(message). The '{}' in the error catalog is just the caller-supplied message string. It exists to convert a plain message into an unchecked exception at a call site without declaring throws.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/exceptions/ExceptionUtil.java:113

	 */
	public static void wrapAndThrow(Throwable throwable) {
		if (throwable instanceof RuntimeException) {
			throw (RuntimeException) throwable;
		}
		if (throwable instanceof Error) {
			throw (Error) throwable;
		}
		throw new UndeclaredThrowableException(throwable);
	}

	/**
	 * 将消息包装为运行时异常并抛出
	 *
	 * @param message 异常消息
	 * @since 5.5.2
	 */
	public static void wrapRuntimeAndThrow(String message) {
		throw new RuntimeException(message);
	}

	/**
	 * 剥离反射引发的InvocationTargetException、UndeclaredThrowableException中间异常,返回业务本身的异常
	 *
	 * @param wrapped 包装的异常
	 * @return 剥离后的异常
	 */
	public static Throwable unwrap(Throwable wrapped) {
		Throwable unwrapped = wrapped;
		while (true) {
			if (unwrapped instanceof InvocationTargetException) {
				unwrapped = ((InvocationTargetException) unwrapped).getTargetException();
			} else if (unwrapped instanceof UndeclaredThrowableException) {
				unwrapped = ((UndeclaredThrowableException) unwrapped).getUndeclaredThrowable();
			} else {
				return unwrapped;
			}

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Decide whether an unchecked RuntimeException is the right type for the condition; if a domain-specific exception exists, throw that instead.
  2. Provide a meaningful, actionable message (include offending values/ids).
  3. Catch it at the appropriate boundary and convert to a user-facing error or retry.

Example fix

// before
ExceptionUtil.wrapRuntimeAndThrow("bad input");
// after - throw a domain exception with context
throw new IllegalArgumentException("bad input: " + value);
Defensive patterns

Strategy: try-catch

Try / catch

try { doRiskyWork(); }
catch (RuntimeException e){ if(e.getMessage()!=null && e.getMessage().equals(yourMsg)) { /* expected business abort */ } else throw e; }

Prevention

When it happens

Trigger: Calling ExceptionUtil.wrapRuntimeAndThrow(someMessage) explicitly in code (e.g. inside a lambda, a stream, or a method whose signature forbids checked exceptions) to abort with a RuntimeException carrying the given message.

Common situations: Stream/lambda pipelines that need to throw on a business condition; defensive 'should never happen' branches; propagating an error message up without a checked-exception signature.


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/9ad38a157165273b. Report an issue: GitHub.