-3

So, I understand from this question that finally blocks can be used to execute code even if an Exception occurs and even if the Exception is uncaught and allowed to propagate up.

I understand from this question that in C#, the throw keyword can be used alone (with no arguments) in a catch block to allow the caught Exception to continue propagating up without even resetting the stack trace.

My question, then, is what is the difference between THESE two blocks:

/* example 1 */
try { /* try stuff */ }
finally { /* finally/catch stuff */ }

/* example 2 */
try { /* try stuff */ }
catch { /* finally/catch stuff */ throw; }

Don't both run the stuff the try stuff, then run the finally/catch stuff, then allow the thrown Exception to propogate up with the same stack trace?

Community
  • 1
  • 1
Variadicism
  • 624
  • 1
  • 7
  • 17

5 Answers5

2

In

try { /* try stuff */ }
catch { /* finally/catch stuff */ throw; }

the finally stuff won't run when there is no error.

A finally{} block is used for cleanup, your suggestion would litter valuable resources.

You really missed the point here, only catch is about handling errors (optionally in stages). A finally block is about resource management and only related to exceptions in the sense that it will execute despite of any exceptions having occurred.

H H
  • 263,252
  • 30
  • 330
  • 514
1

Think of it like this...

try
{
    //do some stuff
}
catch
{
    //do some stuff if there was an exception
    //maybe some cleanup, maybe rethrow exception
}
finally
{
    //always do this stuff exception or not
}
Kevin
  • 157
  • 3
  • Ah, of course. So `finally/catch stuff` will be run in the `finally` block even if there isn't an Exception, while in the `catch` block, it will only be run if there's an Exception. What about the way the Exception itself is handled and propagated if one is thrown, though? Any difference? – Variadicism Apr 10 '15 at 18:36
  • 1
    catch and finally, although parts of the same structure (try/catch/finally) are not related. Try is the block of code that you are going to attempt to execute (maybe sucessful, maybe not). Catch is only for handling the case where the code block in try fails. It allows you to either just swallow the exception (not recommended, but I've seen it done in a service that absolutely must continue running no matter what), wrap the exception in a new exception to add more information and rethrow, or clean up/release things then rethrow. Finally simply defines a block of code that always is run. – Kevin Apr 10 '15 at 18:59
  • What is the use of finally if the catch throws an exception? It won't execute right? – user33276346 Jul 17 '20 at 16:07
0

catch runs only if content of try throws an error, finally runs always after try and/or catch.

dolek
  • 214
  • 1
  • 8
0

The difference is that the finally block is always executed both if you have an exception or not, instead the catch block is executed only if you have an exception

0

A finally will run regardless as to whether your code throws an exception or not, but it is my understanding (i could be wrong) that it may not run if you re-throw in a catch which is in turn not handled. The finally is useful when you need to tidy up regardless of whether an error occurred or not, such as disposing connections.

The second example would only have code run in the catch, or if the code doesn't throw and exception. In which case I would remove the try catch and let it bubble up and have the try catch higher up

Steven Brookes
  • 834
  • 6
  • 27