• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Exceptions and error pages

 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Ranchers!

I've got a problem. I have a servlet, which makes some computations. Let's say that this Servlet can (and will) throw an Illegal Argument Exception. Then I want to wrap this exception into the ServletException and throw it from the doGet(-) method, just like that:



I am also printing the stack trace to be sure what I am throwing, and it shows:

javax.servlet.ServletException: Customer ID is invalid
        at com.example.ocmjd.BookServlet.doGet(BookServlet.java:22)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
(...) // buch of low-level stack trace elements



So, I've got my ServletException and message shown in the stack trace - fine. Now, my web.xml relevant part looks like:



So, the container is thinking - "I've bumped into the ServletException and I (the mighty container) will pass the exception to the /servletException.jsp file to cope with it":



And the result is:

Exception: java.lang.IllegalArgumentException
Message:



So, the exception is IllegalArgumentException - not a ServletException which I expected (as the IAE is just a cause for ServletException) and the message is empty.

Well, it looks like the container (?) is unwrapping the exception I am catching and serving just the cause of it. So the ${pageContext.exception} is the cause, not the real-one thrown exception.

Is it always like that? Can I access the original ServletException which was thrown from the Servlet? And, the most important, why is the container doing this to me? Why...?

Cheers!
 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pedro!

Well, it looks like the container (?) is unwrapping the exception I am catching and serving just the cause of it. So the ${pageContext.exception} is the cause, not the real-one thrown exception.

Is it always like that? Can I access the original ServletException which was thrown from the Servlet? And, the most important, why is the container doing this to me? Why...?



Yes, it is a bit hidden, but this is what the jsp2.0 spec says about it:

Table JSP.1-8 Page Directive Attributes
errorPage
"....If the URL names another JSP page then, when invoked that JSP page's exception implicit script variable shall contain a reference to the originating uncaught Throwable."


Why...?


Because

SRV.9.9.2 Error Pages
To allow developers to customize the appearance of content returned to a Web client
when a servlet generates an error, the deployment descriptor defines a list of error
page descriptions



When you don't define the errorPage in your web.xml you will see the ServletException. It is a possibility to customize the error-handling from exceptions thrown from the business layer.

Regards,
Frits
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks once again Frits :-)

I didn't know about that...

Oh, and BTW I've bumped into something like this:


Servlet 3.0 Final, 10.9.2 Error Pages

If no error-page declaration containing an exception-type fits using the class-hierarchy match, and the exception thrown is a ServletException or subclass thereof, the container extracts the wrapped exception, as defined by the ServletException.getRootCause method. A second pass is made over the error page declarations, again attempting the match against the error page declarations, but using the wrapped exception instead.



I really love this feeling while you learn something new :-)
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Oh, and BTW I've bumped into something like this:
If no error-page declaration containing an exception-type fits using the class-hierarchy match, and the exception thrown is a ServletException or subclass thereof, the container extracts the wrapped exception, as defined by the ServletException.getRootCause method. A second pass is made over the error page declarations, again attempting the match against the error page declarations, but using the wrapped exception instead.


Yes I had also seen that part in Servlet2.4, and I was just wondering what will happen if you have defined both the page for the ServletException and the page for the IllegalArgumentException:
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It stopped at the ServletException, opened servletException.jsp. In this file I print what exception occured and it's IllegalStateException.

So, it's just like the spec says, but I still think it's a bit confusing at least for me :-) In this particular case it's even more confusing ;-)

Cheers!
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic