File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Schandha Ravi
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64200
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic