Since the question talks about a business exception, I think the answer in the mock exam is correct. In this case, the exception-type tag for the error-page tag in the DD would likely have a value of java.lang.Exception, which can always refer to any business exception. [ June 19, 2005: Message edited by: Kedar Dravid ]
I get the business exception error page only when i map 'ServletException to error-page' - which means i get a,d as the answers to the above. When i try mapping 'business exception to error-page' i do not get the intended/mapped error page.
Isn't this the way, it is suppose to behave since we wrap the business exception into ServletException ?
It's all working. And now it's making me a lot sensible also. Here is what I have understood after spending quite some time on 'declarative' exception handling.
Container checks if a servlet throws ServletException, if so, it gets the root cause of that exception by invoking getRootCause() method on it, then it checks if developer has defined any handling for this root cause exception in DD, if so invokes the error page associated with that root cause exception, otherwise throws 500.
We can not throw any non-(IOException/ServletException) from servlet because of inheritance rule. A derived class cannot throw additional non-runtime exceptions than it's super class method does. Hence we are bound to wrap our CustomException object into ServletException object before throwing it.
Here is my code that worked perfactly.
=========================================================== Now my next question on the same line is why can't we extend our CustomException from RuntimeException and throw it from servlet without having to declare in throws clause and define a CustomException error-page mapping in DD. Here is another question from a mock exam: [HR] The deployment descriptor of a web application contains the following:
Assuming that com.exceptions.CustomException extends from java.lang.Exception, which of the following statements are correct?
Select 1 correct option. a This error page mapping will never be used because a servlet can only throw IOException or ServletException. b If a servlet throws CustomException, customerror.html will be shown to the user. c As such, this error page mapping will only be used if a servlet wraps this exception into a ServletException and then throws the ServletException. d This error page mapping will only be used if CustomException is made to extend from RuntimeException. [/HR]
The answer to above question is c but in my understanding c and d both should be correct.
Any idea please? [ June 19, 2005: Message edited by: Anand Wadhwani ]
Joined: Jul 13, 2004
How is option c) mentioned above correct ? For this option to work dont we need to have an explicit mapping such as
This is with reference to your line: "Now my next question on the same line is why can't we extend our CustomException from RuntimeException and throw it from servlet without having to declare in throws clause and define a CustomException error-page mapping in DD. "
Now my question is can we create our own custom Runtime Exception??? I tried to search on net but didn't find any answers!!!
SCJP 1.4 : 91%
SCWCD 1.4 : 95%
SCBCD 1.3 : 95%
SCJP 6 Upgrade : 95%
Next SCBCD 1.5
Man is not finished when he is defeated. He is finished when he quits.