我有一个@ControllerAdvice
类来处理SpringMVC控制器的异常。我想在@ExceptionHandler
方法中捕获已知类型(RuntimeException)的异常,然后抛出e.getCause()
异常,并使同一个@ControllerAdvice类捕获此异常。
示例代码:
@ControllerAdvice
public class ExceptionHandlingAdvice
{
@ExceptionHandler( RuntimeException.class )
private void handleRuntimeException( final RuntimeException e, final HttpServletResponse response ) throws Throwable
{
throw e.getCause(); // Can be of many types
}
// I want any Exception1 exception thrown by the above handler to be caught in this handler
@ExceptionHandler( Exception1.class )
private void handleAnException( final Exception1 e, final HttpServletResponse response ) throws Throwable
{
// handle exception
}
}
这可能吗?
答案 0 :(得分:0)
您可以检查RuntimeException是否是Exception1.class的实例并直接调用该方法:
private void handleRuntimeException( final RuntimeException e, final HttpServletResponse response ) throws Throwable
{
if (e instanceof Exception1) handleAnException(e,response);
else throw e.getCause(); // Can be of many types
}