Servlet:我应该在ServletException中包装所有异常吗?

时间:2014-04-16 13:32:32

标签: java servlets

假设我有一个servlet,它在如下定义的方法中执行所有必要的处理:

protected abstract void process(ServletRequest request, ServletResponse response);

实现 Servlet 接口的正确方法是什么?

ServletException 中包装运行时异常:

protected void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
  try {
    process(request, response);
  } catch(Throwable t){
    throw new ServletException(t);
  }
}       

或按原样抛出运行时异常:

protected void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
  process(request, response);
}

1 个答案:

答案 0 :(得分:-1)

仅在必须处理异常时才包装代码,否则不需要换行。