Try block in Tomcat JSP doesn't appear to "catch" error


T

tfrawley

I am using Tomcat 6.0.18 and JDK 1.6 u10.

I have tested this simple JSP page:

<%@ page language = "java" %>
<hr>
My nice little JSP web page.
<hr>
<%
try{
wtf="this doesn't work!";
}catch (Exception e){
out.println(e.getMessage());
} finally {
} // try
%>

I expected to see the horizontal rule follows by the "My nice little
JSP web page" another horizontal rule and then the error text from the
out.println(e.getMessage());

What I do get is a Tomcat error page instead. If possible I would
like Tomcat to process the entire web page and skip over errors that I
place in try blocks. Is that possible?
 
Ad

Advertisements

O

Owen Jacobson

I am using Tomcat 6.0.18 and JDK 1.6 u10.

I have tested this simple JSP page:

<%@ page language = "java" %>
<hr>
My nice little JSP web page.
<hr>
<%
try{
        wtf="this doesn't work!";}catch (Exception e){

        out.println(e.getMessage());} finally {
} // try

%>

I expected to see the horizontal rule follows by the "My nice little
JSP web page" another horizontal rule and then the error text from the
out.println(e.getMessage());

What I do get is a Tomcat error page instead.  If possible I would
like Tomcat to process the entire web page and skip over errors that I
place in try blocks.  Is that possible?

If that is your exact page, then the error occurs during parsing,
before any of your code runs, and you can do nothing inside the JSP to
change the outcome (except remove the parse error, obviously). You're
really solving the wrong problem: why do you have pages that have
parse errors, and why can't you fix them so that they at least parse
cleanly?

Once your page is actually executing (after it's been parsed and
converted to a Servlet), exception handling within the page will run
as normal.

Also, scriptlets (embedded Java snippets) suck, don't use them. Do
your logic in a controller layer (either servlets or some MVC web
framework) and do only rendering in the JSP, using EL and taglibs
where necessary.

-o
 
L

Lew

Owen said:
If that is your exact page, then the error occurs during parsing,
before any of your code runs, and you can do nothing inside the JSP to
change the outcome (except remove the parse error, obviously).  You're
really solving the wrong problem: why do you have pages that have
parse errors, and why can't you fix them so that they at least parse
cleanly?

OP: What is the type of 'wtf'?

If you're going to use scriptlet (inadvisable, as Owen mentioned), it
has to be syntactically correct.

Also, you really ought to use conventional indentation, especially for
Usenet posts, or any other code you want others to read.
 
T

tfrawley

Ownen,

Yup, your right. I should have thought of that. I do have a
situation where i have this type of problem so I was trying to re-
create the issue in an easy to post fashion.

The real problem I have is I created a java class and compiled it with
javac and it compiled successfully. I instantiated and called a
method of the class successfully and everything was fine. Soo... I
added a couple more methods to the class and compiled it again, which
compiled successfully, however, I noticed that when I re-opened the
JSP page it didn't display the entire page. It cut out about half way
through a section of static content.

I found that by commenting out the class call in the JSP page it would
successfully render the rest of the page. I also found that this same
issue occurred on my work mates computer whos content is simular to
mine and has the same problem.

I will try and re-create the issue with an appropriate set of test
code that I can post here online.

I apologize for the inconvenience.
 
M

Mark Space

What I do get is a Tomcat error page instead. If possible I would
like Tomcat to process the entire web page and skip over errors that I
place in try blocks. Is that possible?

To translate Lew and Owen: The type of error you've shown us here can't
be caught by your code, because your code will never run. Please show us
the correct error code instead. A cut and paste of the full error
message itself would also be very helpful.

If this code is supposed to be some sort of short example, well it
should work (minus all the actual errors) and catch an error for you.
So the problem is somewhere that you haven't shown. Again, we need a
short example that really produces the error, not something made up that
couldn't even be run at all.
 
L

Lothar Kimmeringer

<%
try{
wtf="this doesn't work!";

if whatever happens here produces an Error instead of an Exception
}catch (Exception e){

it will not be catched here leading to the 500-error you see.
In the error-log of your servlet-container you should be able
to read the concrete Error/Exception that happened. That way
you should be able to find the problem without adding debug-
code to your JSP-page.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Ad

Advertisements

Ad

Advertisements

L

Lothar Kimmeringer

I asked the wrong question

see my signature
so please stop posting responses. :)

This is Usenet. Be happy to get answers that remotely have some-
thing to do with a question anyway. ;-)


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 

Top