finally with no try/catch?

Discussion in 'Java' started by kaeli, Sep 14, 2004.

  1. kaeli

    kaeli Guest

    Hey all,

    At the moment, I have a web application that has an error page that catches
    all my errors. This works great. When any of my classes throws an error, the
    app forwards on to error.jsp so users can see the error message.
    The thing is, classes that throw errors when DB connections, files, and
    whatnot are open would get messed up if I didn't close things. So what I did
    was to use a try/catch/finally block so that I could close things in the
    finally block and just re-threw the exception.

    Is there a more elegant way to do this?
    I mean, catching errors just to throw them again is rather silly.
    But if it's the only way I get a finally, it'll have to do.

    The classes in question are beans that are used in a JSP intranet app if it
    matters or helps. No struts, I'm afraid, nor Tomcat.

    She was engaged to a boyfriend with a wooden leg but broke
    it off.
    kaeli, Sep 14, 2004
    1. Advertisements

  2. Why do you catch the Exception in the first place? You _can_ use

    try {
    // ...
    } finally {
    // ...

    just as well. No need to catch anything. In fact, AFAIK this is the main
    use for finally.
    Stefan Schulz, Sep 14, 2004
    1. Advertisements

  3. kaeli

    kaeli Guest

    I thought I HAD to catch it if I was using try.

    This was exactly what I was asking if I could do.

    kaeli, Sep 16, 2004
  4. kaeli

    code learner

    Dec 24, 2010
    Likes Received:
    Hi kaeli,

    as mentioned by Stefan Schulz
    the try-finally block should work.


    code learner, Dec 24, 2010
    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.