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.

    --
    --
    ~kaeli~
    She was engaged to a boyfriend with a wooden leg but broke
    it off.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    kaeli, Sep 14, 2004
    #1
    1. Advertising

  2. kaeli wrote:
    > 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.
    >


    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
    #2
    1. Advertising

  3. kaeli

    kaeli Guest

    In article <ci7mc0$11sq$-bonn.de>,
    enlightened us with...
    > >

    >
    > 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.
    >



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

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

    --
    --
    ~kaeli~
    The secret of the universe is @*&^^^ NO CARRIER
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    kaeli, Sep 16, 2004
    #3
  4. kaeli

    code learner

    Joined:
    Dec 24, 2010
    Messages:
    19
    Hi kaeli,

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


    try{
    }finally{
    }

    bye
    learner
    code learner, Dec 24, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Question: Try,Catch,Finally

    VB Programmer, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    360
    Kevin Spencer
    Aug 7, 2003
  2. VB Programmer

    The problem with Try Catch Finally...

    VB Programmer, Aug 12, 2003, in forum: ASP .Net
    Replies:
    23
    Views:
    765
    Chad Myers
    Aug 15, 2003
  3. Ralph Krausse

    ALL 'try/catch/finally' NOT created equal?

    Ralph Krausse, Aug 20, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    410
  4. hansiman

    try catch finally

    hansiman, Jul 14, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    480
    Sean M
    Jul 14, 2005
  5. David Lozzi

    Try...Catch...Finally not firing finally?

    David Lozzi, Apr 23, 2007, in forum: ASP .Net
    Replies:
    12
    Views:
    783
    Alvin Bruney [MVP]
    May 11, 2007
Loading...

Share This Page