Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does try/finally ignore exceptions?

I have a situation where I want certain code to be executed no matter what happens, but I need exceptions to also be passed on up the stack to be handled later. Is the following:


try
{
  // code
}
finally
{
  // code that must run
}

going to just ignore any exceptions, or will it pass them on up? My testing seems to show that they still get passed on up, but I want to be sure I'm not crazy.

EDIT: My question isn't about when and if the finally will execute, it's about whether exceptions still get thrown upwards, but that's been answered now.

like image 939
Matt H Avatar asked Jul 06 '09 23:07

Matt H


2 Answers

The finally code will always run, and exceptions will be passed on up, as you say. That's pretty much the point of try/finally - to have some code that will always run, even when exceptions are thrown.

Edit: This is true for any language that provides the try/finally construct, but there are caveats for some languages, as Adam points out in his comment and Sam points out in his answer.

like image 61
RichieHindle Avatar answered Jan 19 '23 19:01

RichieHindle


Here's a test class that shows that (1) finally runs, regardless of whether exceptions are thrown; and (2) exceptions are passed along to the caller.

public class FinallyTest extends TestCase {
    private boolean finallyWasRun   = false;

    public void testFinallyRunsInNormalCase() throws Exception {
        assertFalse(finallyWasRun);
        f(false);
        assertTrue(finallyWasRun);
    }

    public void testFinallyRunsAndForwardsException() throws Exception {
        assertFalse(finallyWasRun);
        try {
            f(true);
            fail("expected an exception");
        } catch (Exception e) {
            assertTrue(finallyWasRun);
        }
    }

    private void f(boolean withException) throws Exception {
        try {
            if (withException)
                throw new Exception("");
        } finally {
            finallyWasRun = true;
        }
    }
}
like image 20
Carl Manaster Avatar answered Jan 19 '23 18:01

Carl Manaster