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)
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).