java-ExceptionHandler全局异常处理
springmvc配置文件:
<!-- 定义全局异常处理,只有一个全局异常处理器起作用 --> <bean id="exceptionResolver" class="net.nblh.utils.common.OverallExceptionResolver"></bean>
OverallExceptionResolver:
package net.nblh.utils.common; import org.apache.log4j.Logger; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; /** * @author lijd * */ @ControllerAdvice//Controller增强类 public class OverallExceptionResolver { private static final Logger logger = Logger.getLogger(OverallExceptionResolver.class); /** * 处理所有不可知的异常 * @param e */ @ExceptionHandler(Exception.class) public static void handlerException(Exception e) { String stackTrace = e.getStackTrace().length>0?e.getStackTrace()[0].toString():""; String errorMsg = "系统未捕获的异常handlerException:error:"+e.toString()+"\n"+"stackTrace:"+stackTrace; System.out.println(errorMsg); logger.error(errorMsg); } }