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

Discussion in 'Java' started by tfrawley, Nov 5, 2008.

  1. tfrawley

    tfrawley Guest

    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?
     
    tfrawley, Nov 5, 2008
    #1
    1. Advertisements

  2. 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
     
    Owen Jacobson, Nov 5, 2008
    #2
    1. Advertisements

  3. tfrawley

    Lew Guest

    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.
     
    Lew, Nov 5, 2008
    #3
  4. tfrawley

    tfrawley Guest

    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.
     
    tfrawley, Nov 5, 2008
    #4
  5. tfrawley

    Mark Space Guest

    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.
     
    Mark Space, Nov 5, 2008
    #5
  6. if whatever happens here produces an Error instead of an Exception
    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:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
     
    Lothar Kimmeringer, Nov 6, 2008
    #6
  7. tfrawley

    tfrawley Guest

    I asked the wrong question so please stop posting responses. :)
     
    tfrawley, Nov 6, 2008
    #7
  8. see my signature
    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:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
     
    Lothar Kimmeringer, Nov 6, 2008
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.