【发布时间】:2013-04-15 11:30:30
【问题描述】:
我已经开始主要基于 Struts 和 Servlet 开发 Java EE Web 应用程序。大多数代码在 Servlet 或 Struts Action 类中都有一个 try catch 块。
是否必须为每个 servlet 或操作设置 try catch 块?我看到这种代码模板的唯一优点是堆栈跟踪是日志到应用程序指定的日志框架,例如 log4j。
如果运行时异常上浮,则改为打印在服务器(Tomcat / Glassfish / Weblogic)日志上。
public class HelloWorldAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception {
try {
// do all the processing here
} catch (Exception e) {
// log all exceptions
}
}
}
public class HelloWorldExample extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
// do all the processing here
} catch (Exception e) {
// log all exceptions
}
}
}
【问题讨论】:
-
如果您现在开始,请使用 Struts2,而不是 Struts !除非您使用 Java 1.4,否则您正在开发 Java EE webapp,而不是 J2EE:en.wikipedia.org/wiki/…
标签: java exception servlets error-handling struts