自定义的AppException implements RuntimeException, 在抛出时被包装了NestedServletException,这样就无法被 catche(AppException ae)捕捉到,只能被Exception捕捉到。。。有没有办法,不让随便“被包装”,而是我抛什么就是什么。
有个方案是 catch exception e,然后再 e.getRootCause ,但是这样,无法 使用catch AppException ae,只能 catch Exception e,然后再做if判断,感觉这样不爽。
if (e instanceof NestedServletException) {//加了spring security就被包装成这个exception了
if (((NestedServletException) e).getRootCause() instanceof BizException) {
logger.error(e.getClass().getName(), e);
result = ResultUtils.errorResult(((BizException) ((NestedServletException) e).getRootCause()).getCode(),
((NestedServletException) e).getRootCause().getLocalizedMessage());
} else {
logger.error(" error ", e);
// TODO 未知的异常,应该格外注意,可以发送邮件通知等
result = ResultUtils.serverError();
}
}
BizException只是简单继承只runtimeexception而已