tags:

views:

120

answers:

8

Hello. I have the following code:

            try {
                fi.MoveTo(getHistoryFileName());
            } finally {
                Debug.Write("Move complete");
            }

I thought that using this would make sure that I'd never have exceptions thrown, but I am getting IOExceptions sometimes. What is the reason?

+7  A: 

finally does not catch thrown exceptions; it guarantees that code in it will execute regardless of whether an exception is thrown in the try block.

You are going to want to catch the exceptions and then handle them appropriately. A Catch block would be between the Try and Finally blocks and start with something like this:

catch(Exception ex)
{
     //error handling
}

You could replace Exception with a more specific exception to just handle that exception.

Matthew Jones
+3  A: 

It means that the finally block will always get executed, even if there are exceptions.

You still need a catch (IOException) block before the finally to handle the exceptions you don't want to propagate.

try {
    fi.MoveTo(getHistoryFileName());
} catch (Exception) {
  // eat exceptions.
} finally {
    Debug.Write("Move complete");
}
lavinio
+4  A: 

The try/finally combination doesn't handle exceptions like you think it does.

All it does is ensure, as much as possible, that the code in the finally block will run, even in cases when an exception is thrown in the code in the try block.

To avoid specific exceptions, handle them, like this:

try {
    fi.MoveTo(getHistoryFileName());
} catch (IOException ex) {
    // swallow this one
} finally {
    Debug.Write("Move complete");
}
Lasse V. Karlsen
Note the "//swalow this one" comment, when catching an exception and doing nothing with it, always a good idea to put some sort of comment in there indicating that you did it on purpose so you or someone else knows it was a concious decision 6 months or a year from now.
James Conigliaro
@James: agree. Also helpful would be a reason why it was swallowed.
Michael Petrotta
+2  A: 

You have no catch clause, so no exceptions are getting caught.

The finally clause doesn't swallow exceptions, it just gets executed before the exception is thrown up to the calling function.

That and swallowing exceptions is a bad idea. You shouldn't swallow exceptions, but rather handle them appropriately and gracefully. At the very least they ought to be logged for debugging purposes.

Welbog
+1  A: 

Rewrite that to be:

try {
  fi.MoveTo(getHistoryFileName());
} catch (Exception ex) {
  // do something about it.
} finally { 
Debug.Write("Move complete");
}
Chris Lively
+1  A: 

you use a catch to capture any exceptions. Finally just ensures that even if an exception is thrown, its contents will execute. (Good for cleaning up even if something goes wrong).

statenjason
+1  A: 

where is your catch ?

try {
 fi.MoveTo(getHistoryFileName());
}
catch (IOException ex) {
//log it  and take apporpriate action
} finally {
Debug.Write("Move complete");
}
Adinochestva
+2  A: 

As others have said, if you want to stop unhandled exceptions from bubbling up you need a catch block. But I'm troubled by your question. You worded it like this:

never have exceptions thrown

This is subtly wrong. You can never stop an exception from being thrown. All you can do is handle it so it doesn't make it to the user. I might even let that go, because it's hard to phrase that well semantically. But then you also have the "Move complete" line in your finally block. Put those together, and it makes me wonder if you think that just by catching the exception you can force your try block code to succeed. And that is just dangerous. Don't just swallow the exception and then report to the user that everything went as expected.

Joel Coehoorn