简体   繁体   English

Java EE / Struts异常处理

[英]Java EE / Struts Exception Handling

I have started developing Java EE web applications mainly on Struts and Servlets. 我已经开始主要在Struts和Servlets上开发Java EE Web应用程序。 Most of the codes have a try catch block within Servlet or Struts Action class. 大多数代码在Servlet或Struts Action类中都有一个try catch块。

Is it a must to have try catch block for every servlet or action? 每个servlet或操作都必须有try catch块吗? The only advantages I saw with this kind of code template is stacktrace are log to application specified logging framework such as log4j. 我用这种代码模板看到的唯一优点是stacktrace是将日志记录到应用程序指定的日志框架,例如log4j。

If the runtime exception floats up, it will be printed on the server (Tomcat / Glassfish / Weblogic) logs instead. 如果运行时异常浮动,它将被打印在服务器(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
    }
   }
}
  1. Catching Exception is almost never what you really want to do. 捕获Exception几乎从来不是您真正想要做的。 Hopefully it's obvious that it's not mandatory to always have a try/catch block–it depends on what the underlying code is doing, and how you want to handle any exceptions it may throw. 希望很明显, 始终拥有try / catch块不是强制性的-它取决于基础代码在做什么,以及您希望如何处理它可能引发的异常。

  2. Catching Exception eliminates the ability to use Struts' declarative exception handling . 捕获Exception消除了使用Struts的声明式异常处理的能力

I would recommend against using a filter to handle exceptions in Struts 1 since it already has a mechanism built in. If there are exceptions at the framework level they'll be displayed anyway, and they generally indicate a development, not runtime, issue. 我建议不要使用过滤器来处理Struts 1中的异常,因为它已经内置了机制。如果在框架级别存在异常,则无论如何都将显示它们,并且它们通常表示开发而不是运行时问题。

I echo Andrea's sentiments: unless you have a Very Good Reason, learning Struts 1 isn't useful. 我赞同安德里亚的观点:除非您有很好的理由,否则学习Struts 1不会有用。 Consider instead Struts 2 or Spring MVC for "traditional" framework development, or Play, Grails, JRuby on Rails, etc. for a more modern approach. 可以考虑将Struts 2或Spring MVC用于“传统”框架开发,或者考虑使用Play,Grails,JRuby on Rails等,以获得更现代的方法。

If you are looking for one place to do exception logging, you can create ServletFilter: 如果您正在寻找一个进行异常日志记录的地方,则可以创建ServletFilter:

public class ExceptionLoggerFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) {
        try {
            filterChain.doFilter(req, res);
        }
        catch(Exception e) { // Log exception }
    }
 }

You shouldn't really have to catch exceptions everywhere unless you want to handle that particular exception in a special way. 除非您想以一种特殊的方式处理该特定异常,否则您实际上不必在任何地方捕获异常。 Most of the time it's just noise getting in the way for the "real" code. 在大多数情况下,这只是干扰“真实”代码的方式。 The important thing is that you log the exception and enough context information that you can figure out what caused the error. 重要的是,您记录了异常和足够的上下文信息,您可以找出导致错误的原因。 To the user, you should probably just display a general error page. 对于用户,您可能应该只显示一个常规错误页面。

You can specify global exception in struts-config.xml . 您可以在struts-config.xml指定全局异常 It catches unhandled exceptions all over the application. 它捕获整个应用程序中未处理的异常。 You need to implement your own ExceptionHandler class. 您需要实现自己的ExceptionHandler类。 Then write code what to want to do. 然后编写代码要做什么。

4.5 Exception Handler http://www.jajakarta.org/struts/struts1.2/documentation/ja/target/userGuide/building_controller.html 4.5异常处理程序 http://www.jajakarta.org/struts/struts1.2/documentation/ja/target/userGuide/building_controller.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM