假设我有一个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);
}
答案 0 :(得分:-1)
仅在必须处理异常时才包装代码,否则不需要换行。