1

I have started developing Java EE web applications mainly on Struts and Servlets. Most of the codes have a try catch block within Servlet or Struts Action class.

Is it a must to have try catch block for every servlet or action? The only advantages I saw with this kind of code template is stacktrace are log to application specified logging framework such as log4j.

If the runtime exception floats up, it will be printed on the server (Tomcat / Glassfish / Weblogic) logs instead.

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
  • If you are starting now, please use Struts2, not Struts !! And unless you are using Java 1.4, you are developing a Java EE webapp, not a J2EE one: en.wikipedia.org/wiki/… Commented Apr 22, 2013 at 14:13

3 Answers 3

4
  1. Catching Exception is almost never what you really want to do. 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.

  2. Catching Exception eliminates the ability to use Struts' declarative exception handling.

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.

I echo Andrea's sentiments: unless you have a Very Good Reason, learning Struts 1 isn't useful. Consider instead Struts 2 or Spring MVC for "traditional" framework development, or Play, Grails, JRuby on Rails, etc. for a more modern approach.

Sign up to request clarification or add additional context in comments.

4 Comments

i was writing my answer while i saw yours poped.. ;-) +1 for the use of Struts Exception Handling. My rules of thumb in web app is to throw every exceptions at the controller (Action) and deal with them using ExceptionHandler. My eyes always flickerd when i see try..catch block !
You are neglecting legacy code and there is still lots of it. Struts 1x is useful in the sense that there is still a lot of legacy code using it. I'm working in an environment where they are using both Struts 1 and 2 in the same shop. We are in the process of migrating all the apps up to Struts 2 when it's cost feasible.
@JamesDrinkard I'm not neglecting anything; I specifically stated "unless you have a Very Good Reason".
@DaveNewton Correct, and that would definitely qualify. Thanks for pointing that out!
1

If you are looking for one place to do exception logging, you can create 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.

Comments

0

You can specify global exception in struts-config.xml. It catches unhandled exceptions all over the application. You need to implement your own ExceptionHandler class. 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.