File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to capture the exceptions thrown from third party services in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to capture the exceptions thrown from third party services in servlet" Watch "How to capture the exceptions thrown from third party services in servlet" New topic
Author

How to capture the exceptions thrown from third party services in servlet

Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Hello All,

Assume I'm calling a method of a third party enterprise service of many layers. Now this method throws back an exception to the servlet in doGet method, from which the enterprise method is called. Since doGet() method already throws ServletException and IOException and we can not throw any other exception apart from these two. Now my question is, how do we want to throw the exception which we get by calling the other services. Is it appropriate to extend either ServletException or IOException, and encapsulate our own exception into this.

I better put my question as the below code, to make it more clear



Now obviously for this code to run, MyOwnException should be either ServletException or IOException, or I need to catch it in doGet() method.

I think of few options below. Please let me know, if these options are fine.
1) Making MyOwnException extending ServletException or IOException.
2) Catch the exception, and in catch block, log the exception message using standard loggers and making it accessible to the user for display purpose.
3) catch the exception and in catch block, throw ServletException with MyOwnException.printStackTrace() or any other meaningful information as the argument for constructor.

I'm sure there would be a better solutions than these. Please share the same.


Thanks & Regards, SK
SCJP 5.0, DB2 - 800, DB2 - 803, SCDJWS (On the way)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
Catch the exception, and in catch block, log the exception message using standard loggers and making it accessible to the user for display purpose.

This sounds like a sensible approach. I don't see a need to throw an exception here - that would indicate that the servlet has a problem, which is not the case.


Ping & DNS - my free Android networking tools app
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61418
    
  67

I always wrap the exception in a ServletException (no need to extend) and throw that. That way, the error handling declared in the deployment descriptor can handle the exception in a consistent fashion (and shows the original exception as the root cause).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to capture the exceptions thrown from third party services in servlet