異常與日誌
阿新 • • 發佈:2018-02-13
直接 pan test 構造 logger exce ide eth pre
在程序中如果遇到異常,摘取幾個片斷進行說明:
1.直接拋出底層異常,不打印日誌,但前提是底層異常提供了 Xxxx(String msg)這樣的構造方法,以便拋出時可以進一肯細化異常信息,以便調用方明確為什麽發生異常。
protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException { for (HandlerAdapter ha : this.handlerAdapters) { if (logger.isTraceEnabled()) { logger.trace("Testing handler adapter [" + ha + "]"); } if (ha.supports(handler)) { return ha; } } throw new ServletException("No adapter for handler [" + handler + "]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler"); }
2.打印異常,並拋出異常
@Override public void process() throws BankApiException { try { BankRequest request = context.getRequest() ; String url = request.obtainUrl() ; context.setUrl(url); }catch(Exception e) { LOGGER.error(e.getMessage(), e);// 記錄底層的異常 //拋出新的轉譯後的異常通知調用者 throw new BankApiException(BankApiErrType.URLCHECKERR.getValue(),"獲取API URL失敗") ; } }
異常與日誌