aspose file tools*
The moose likes Servlets and the fly likes catch Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "catch Error" Watch "catch Error" New topic
Author

catch Error

Harry Jones
Greenhorn

Joined: Mar 19, 2010
Posts: 20
I know a lot has been written about this subject and I have tried to educate myself. The API and most people, it seems, state that you should not catch an error because they are too serious to recover from. But some people make exceptions, and I am unsure about my situation.

I have a small web application that accesses and updates MySQL without pooled connections and sends out emails from time-to-time using multiple threads. This is the general form of my servlets:

Is trying to catch an error like this a reasonable approach? Will it even work? If I stop doing this in my servlets, what will happen if an error occurs?
Thank you.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

IMO, it is useful to know what is going wrong in at very high level . so some time, you can even catch some Error (for instance AssertionError) , though it is not recommended in javadoc.
I would have replace

with
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

Are you going to recover from the exception in some way? By providing a default in case of failure, perhaps? Retry an operation? Some other recovery scheme?

If not, then don't catch it. All you're doing in that case is getting in the way.

Define a top-level error handler in the deployment descriptor and let it log any exceptions and send the user to a customer-facing page that explains that the error occurs. Under no circustances expose the user to something like a stack trace.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Harry Jones
Greenhorn

Joined: Mar 19, 2010
Posts: 20
I'm not sure whether your question is directed to me or Mr. Venkatasamy. After I close resources and invalidate the session, I send the user to a JSP with "A system error interrupted processing ..."
But, no, I don't try to recover from the error itself.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

If you are not recovering from the error, you shouldn't catch it. Use the deployment descriptor mechanism as I described.
Harry Jones
Greenhorn

Joined: Mar 19, 2010
Posts: 20
I'll pursue your recommendation. Thanks to all who responded. I appreciate it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: catch Error
 
Similar Threads
error handling servlet?
Trying to figure out how to use MySQL and generated @Id
IllegalStateException after canceling file downloading.
Session Listener not getting invoked
Best way to setup / configure Tomcat 7 web app with Hibernate 3 / JPA