1直接捕获
try {// 链接数据库的代码
} catch (Exception e) {
if (e instanceof MySQLTimeoutException) {
System.out.println("数据库连接超时")
}
}
2现在项目都使用Spring连接到数据库,可以在拦截器里面捕获异常,
public class WorkerInterceptor implements HandlerInterceptor {// 最终拦截, 视图已经解析完毕, 监控一些异常,
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception exception) throws Exception {
if (exception instanceof MySQLTimeoutException) {
System.out.println("数据库连接超时")
}
}
// 后置拦截, 可以给使用当前拦截器的追加一些东西, Controller 执行完, 但是视图解析器没有解析成页面,
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView mav) throws Exception {
}
// 前置拦截, Controller 执行前,
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
return true
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)