3

The attribute javax.servlet.error.exception in request with dispatcher type is ERROR can always cast to java.lang.Exception type?

I take a look at the javadoc, but it doesn't mention the type must be java.lang.Exception OR java.lang.Throwable.

I am asking this question because I am handling error page. If the attribute javax.servlet.error.exception always hold Exception type so I can write code as below without worrying about ClassCastException for all cases.

Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");

Of course I can do like this:

Throwable error = (Throwable)request.getAttribute("javax.servlet.error.exception");

but my handling is on Exception type ( Not Throwable ).

Thanks!

LHA
  • 9,398
  • 8
  • 46
  • 85
  • I am not sure what you are asking. ERROR_EXCEPTION_TYPE at the link you provided is of type `String`. – Würgspaß Oct 07 '15 at 14:43
  • Thank you! I typed wrong attribute name. I fixed it. – LHA Oct 07 '15 at 14:49
  • After your edit I still find the question somewhat misleading: "javax.servlet.error.exception" is the constant value of `RequestDispatcher.ERROR_EXCEPTION`. But your main concern apparently is the behaviour of method `ServletRequest.getAttribute(String)`. And this is where `web.xml` and/or `ServletRequest.setAttribute(String, Object)` come into play. – Würgspaß Oct 07 '15 at 15:36
  • @ Würgspaß: But servlet container do setAttribute automatically for exception information when the container does ERROR dispatch – LHA Oct 07 '15 at 15:41
  • Thats right. Anyway, there could be some call to `setAttribute` in the custom server code, as well. Or even someone is going to change the setting in `web.xml` in the future. So, I'd prepare my code for that to be on the safe side... In any case, the issue is all about getAttribute/setAttribute, really. That was my point here. – Würgspaß Oct 07 '15 at 15:50

2 Answers2

1

but my target is Exception

You can use ERROR_EXCEPTION_TYPE to find out the type and then retrieve the object using ERROR_EXCEPTION

String excType = request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE);
if ("java.lang.Exception".equals(excType)) {
   Exception exception = (Exception) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
   //....
}

This could still raise a ClassCastException, however, if ServletRequest.setAttribute() was used in a mischievous way, for example.

If you care for other types as well, you could use a switch or reflection.

Würgspaß
  • 4,660
  • 2
  • 25
  • 41
0

That depends on the <exception-type> associated with <error-page> entry in web.xml whose <location> refers the JSP/Servlet based resource in question.

If there's none, i.e. you're using a general error page without any <exception-type>, or you're using only <error-code>500</error-code>, or you don't have any <error-page> at all, then assume java.lang.Throwable.

If there's an explicit <exception-type>, then assume exactly that type registered in web.xml.

Differently put, if you want to assume only java.lang.Exception, then set <exception-type> accordingly to that type.

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/your-error-resource</location>
</error-page>

Beware though that this way any java.lang.Error (i.e. matching java.lang.Throwable) will slip through and end up in server's default error page when you don't have an <error-page> for 500 or java.lang.Throwable.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555