Stack trace ALL exceptions?

K

kaeli

Hey all,

In my code (JSP), when I catch an exception and print the stack trace, it
isn't giving me the full error like I see in the server logs.
Any hints on how to get the entire error message? (the "caused by" part most
important)

For example, looking at this message, WAY at the bottom, you can see the real
problem was the unparseable date.

The server logs:
[03/Nov/2004:08:33:03] failure (13157): Internal error: servlet service
function had thrown ServletException (uri=/ops_b
eta/events_retrofit_updateNow.jsp): org.apache.jasper.JasperException, stack:
org.apache.jasper.JasperException
at org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper
(JspRuntimeLibrary.java:199)
at org.apache.jasper.runtime.JspRuntimeLibrary.introspect
(JspRuntimeLibrary.java:146)
at _jsps._ops_beta._events_retrofit_updateNow_jsp._jspService
(_events_retrofit_updateNow_jsp.java:138)
at org.apache.jasper.runtime.HttpJspBase.service
(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
com.iplanet.server.http.servlet.NSServletRunner.invokeServletService
(NSServletRunner.java:915)
at com.iplanet.server.http.servlet.NSServletRunner.Service
(NSServletRunner.java:483)
, root cause: java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor15.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper
(JspRuntimeLibrary.java:195)
at org.apache.jasper.runtime.JspRuntimeLibrary.introspect
(JspRuntimeLibrary.java:146)
at _jsps._ops_beta._events_retrofit_updateNow_jsp._jspService
(_events_retrofit_updateNow_jsp.java:138)
at org.apache.jasper.runtime.HttpJspBase.service
(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at
com.iplanet.server.http.servlet.NSServletRunner.invokeServletService
(NSServletRunner.java:915)
at com.iplanet.server.http.servlet.NSServletRunner.Service
(NSServletRunner.java:483)
Caused by: java.text.ParseException: Unparseable date: " "
at java.text.DateFormat.parse(DateFormat.java:334)
at OPS_Beans_20.OPS_EventBean.setSchPrelimDueDate
(OPS_EventBean.java:435)
... 10 more


Yet when I just do exception.printStackTrace in the JSP code, I get only the
first few lines, thus making it impossible to see what the problem was.

From the JSP:
Message: null
Stack Trace: org.apache.jasper.JasperException at
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper
(JspRuntimeLibrary.java:199) at
org.apache.jasper.runtime.JspRuntimeLibrary.introspect
(JspRuntimeLibrary.java:146) at _jsps._ops_beta.
_events_retrofit_updateNow_jsp._jspService
(_events_retrofit_updateNow_jsp.java:148) at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at
com.iplanet.server.http.servlet.NSServletRunner.invokeServletService
(NSServletRunner.java:915) at
com.iplanet.server.http.servlet.NSServletRunner.Service
(NSServletRunner.java:483)

JSP code:
catch (Exception e)
{
String errMsg = e.getMessage();
%>
<p class="bigAttention">Message: <%= errMsg %></p>
<p>Stack Trace:
<%
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
out.print(sw);
sw.close();
pw.close();
}

--
--
~kaeli~
Never argue with an idiot! People may not be able to tell
you apart.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
S

Sudsy

kaeli said:
<snip>

Hey back at ya!
The server logs:
[03/Nov/2004:08:33:03] failure (13157): Internal error: servlet service
function had thrown ServletException (uri=/ops_b
eta/events_retrofit_updateNow.jsp): org.apache.jasper.JasperException, stack:
<snip>

Note that you've received a JasperException. Using javap on jasper.jar shows
that the exception extends ServletException. Using javap on servlet.jar
reveals a method named getRootCause. So now we know enough to extend your
code.
JSP code:
catch (Exception e)
{
String errMsg = e.getMessage();
%>
<p class="bigAttention">Message: <%= errMsg %></p>
<p>Stack Trace:
<%
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
out.print(sw);

if( e instanceof ServletException ) {
pw.println( "Root cause:" );
( (ServletException) e ).getRootCause().printStackTrace( sw );
}
sw.close();
pw.close();
}

Do I even need to mention that the javadocs (and javap) are your
friends? ;-)
 
K

kaeli

The server logs:
[03/Nov/2004:08:33:03] failure (13157): Internal error: servlet service
function had thrown ServletException (uri=/ops_b
eta/events_retrofit_updateNow.jsp): org.apache.jasper.JasperException, stack:
<snip>

Note that you've received a JasperException. Using javap on jasper.jar shows

javap?
I'll have to look up that little dandy.
that the exception extends ServletException.

Apparently not for Netscape Enterprise Server it doesn't. The code failed.
Well, more specifically, the instanceof evaluated to false, so it didn't do
anything bad, but didn't help, either.
I'm going to play with this a bit...

Thanks for the hint.


--
--
~kaeli~
Never argue with an idiot! People may not be able to tell
you apart.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
K

kaeli

Okay, I just had to make sure I didn't write to out until the end. This works
pretty well, but there are two things I need...

Code:
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);

if ( exception instanceof ServletException )
{
pw.println( "Root cause:" );
((ServletException) exception ).getRootCause().printStackTrace( pw );
}
out.print(sw);
sw.close();
pw.close();

This message has "10 more" in it. I want those other lines.

Stack Trace: org.apache.jasper.JasperException at
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper
(JspRuntimeLibrary.java:199) at
org.apache.jasper.runtime.JspRuntimeLibrary.introspect
(JspRuntimeLibrary.java:146) at _jsps._ops_beta.
_events_retrofit_updateNow_jsp._jspService
(_events_retrofit_updateNow_jsp.java:138) at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at
com.iplanet.server.http.servlet.NSServletRunner.invokeServletService
(NSServletRunner.java:915) at
com.iplanet.server.http.servlet.NSServletRunner.Service
(NSServletRunner.java:483) Root cause:
java.lang.reflect.InvocationTargetException at
sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke
(Method.java:324) at
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper
(JspRuntimeLibrary.java:195) at
org.apache.jasper.runtime.JspRuntimeLibrary.introspect
(JspRuntimeLibrary.java:146) at _jsps._ops_beta.
_events_retrofit_updateNow_jsp._jspService
(_events_retrofit_updateNow_jsp.java:138) at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at
com.iplanet.server.http.servlet.NSServletRunner.invokeServletService
(NSServletRunner.java:915) at
com.iplanet.server.http.servlet.NSServletRunner.Service
(NSServletRunner.java:483) Caused by: java.lang.IllegalArgumentException:
Network Number not valid. at OPS_Beans_20.OPS_EventBean.setNetworkNumber
(OPS_EventBean.java:150) ... 10 more


Also, I want to get the text after "Caused by" (network number not valid). Do
you know where that's coming from?
For some reason, when I try
exception.getMessage()
or even
((ServletException) exception ).getRootCause().getMessage()
I get null, so I want to put that text on the screen so the user can easily
see what the problem was. Or is there something I am missing to get the
original message from the original exception?

Thanks for your help!


--
--
~kaeli~
Not one shred of evidence supports the notion that life is
serious.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
S

Sudsy

kaeli wrote:
Also, I want to get the text after "Caused by" (network number not valid). Do
you know where that's coming from?
For some reason, when I try
exception.getMessage()
or even
((ServletException) exception ).getRootCause().getMessage()
I get null, so I want to put that text on the screen so the user can easily
see what the problem was. Or is there something I am missing to get the
original message from the original exception?

My mistake: the sw should have been pw in the code I posted (slaps self
silly).
I don't know how to get the "10 more lines..." message without creating
a filter; probably more trouble than it's worth.
As to the message, Exception#getMessage doesn't always return a message.
I've noticed that it sometimes returns null in my own code. I tend to
revert to the toString method. YMMV.
 
K

kaeli

My mistake: the sw should have been pw in the code I posted (slaps self
silly).

I know, I caught that. Easy mistake.
I don't know how to get the "10 more lines..." message without creating
a filter; probably more trouble than it's worth.
Darn.

As to the message, Exception#getMessage doesn't always return a message.
I've noticed that it sometimes returns null in my own code. I tend to
revert to the toString method. YMMV.

I got null with toString, too, which was why I wanted the text from the
original exception.
I'll keep playing. If I find an easy solution, I'll post it, just in case
anyone else has this issue.

Thanks for the help.

--
 
K

kaeli

Okay, this seems to be working rather well right now.
So, I'll consider it the solution until something else blows up. *LOL*

It gets the original exception, as desired, even past the servletexception,
but handles them if the exception isn't nested or isn't a servletexception,
too.

// unwrap nested exceptions.
while (exception instanceof ServletException ||
exception instanceof InvocationTargetException)
{
if (exception instanceof ServletException)
exception = ((ServletException) exception).getRootCause();
else
exception = ((InvocationTargetException) exception).getCause();
}

errMsg = exception.getMessage();
errMsg = errMsg==null?exception.toString():errMsg;
%>
<p class="bigAttention">Message: <%= errMsg %></p>
<p>Stack Trace:
<%
// print stack trace.
exception.printStackTrace(new PrintWriter(out));
%>
</p>


Output was, as desired:

An exception occurred:
Message: Unparseable date: " "

Stack Trace: java.text.ParseException: Unparseable date: " " at
java.text.DateFormat.parse(DateFormat.java:334) at OPS_Beans_
20.OPS_EventBean.setSchPrelimDueDate(OPS_EventBean.java:435) at
sun.reflect.GeneratedMethodAccessor15.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke
(Method.java:324) at
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper
(JspRuntimeLibrary.java:195) at
org.apache.jasper.runtime.JspRuntimeLibrary.introspect
(JspRuntimeLibrary.java:146) at _jsps._ops_beta.
_events_retrofit_updateNow_jsp._jspService
(_events_retrofit_updateNow_jsp.java:138) at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at
com.iplanet.server.http.servlet.NSServletRunner.invokeServletService
(NSServletRunner.java:915) at
com.iplanet.server.http.servlet.NSServletRunner.Service
(NSServletRunner.java:483)

--
--
~kaeli~
Cthulhu saves our souls and redeems them for valuable
coupons later.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top