{"record":{"id":"9ad38a157165273b","repo":"chinabugotech/hutool","slug":"error","errorCode":null,"errorMessage":"{}","messagePattern":"\\{\\}","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/exceptions/ExceptionUtil.java","lineNumber":113,"sourceCode":"\t */\n\tpublic static void wrapAndThrow(Throwable throwable) {\n\t\tif (throwable instanceof RuntimeException) {\n\t\t\tthrow (RuntimeException) throwable;\n\t\t}\n\t\tif (throwable instanceof Error) {\n\t\t\tthrow (Error) throwable;\n\t\t}\n\t\tthrow new UndeclaredThrowableException(throwable);\n\t}\n\n\t/**\n\t * 将消息包装为运行时异常并抛出\n\t *\n\t * @param message 异常消息\n\t * @since 5.5.2\n\t */\n\tpublic static void wrapRuntimeAndThrow(String message) {\n\t\tthrow new RuntimeException(message);\n\t}\n\n\t/**\n\t * 剥离反射引发的InvocationTargetException、UndeclaredThrowableException中间异常，返回业务本身的异常\n\t *\n\t * @param wrapped 包装的异常\n\t * @return 剥离后的异常\n\t */\n\tpublic static Throwable unwrap(Throwable wrapped) {\n\t\tThrowable unwrapped = wrapped;\n\t\twhile (true) {\n\t\t\tif (unwrapped instanceof InvocationTargetException) {\n\t\t\t\tunwrapped = ((InvocationTargetException) unwrapped).getTargetException();\n\t\t\t} else if (unwrapped instanceof UndeclaredThrowableException) {\n\t\t\t\tunwrapped = ((UndeclaredThrowableException) unwrapped).getUndeclaredThrowable();\n\t\t\t} else {\n\t\t\t\treturn unwrapped;\n\t\t\t}","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/exceptions/ExceptionUtil.java#L95-L131","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Decide whether an unchecked RuntimeException is the right type for the condition; if a domain-specific exception exists, throw that instead.","Provide a meaningful, actionable message (include offending values/ids).","Catch it at the appropriate boundary and convert to a user-facing error or retry."],"exampleFix":"// before\nExceptionUtil.wrapRuntimeAndThrow(\"bad input\");\n// after - throw a domain exception with context\nthrow new IllegalArgumentException(\"bad input: \" + value);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { doRiskyWork(); }\ncatch (RuntimeException e){ if(e.getMessage()!=null && e.getMessage().equals(yourMsg)) { /* expected business abort */ } else throw e; }","preventionTips":["Prefer throwing a specific domain exception over wrapRuntimeAndThrow.","Keep the message constant and searchable for upstream catch logic.","Document the unchecked nature at the call site."],"tags":["exception","utility","runtime","control-flow"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}