Does code in a finally get executed if I have a return in my catch() in c#?
c#, try-catch-finally
Solution
Within a handled exception, the associated finally block is guaranteed to be run. However, if the exception is unhandled, execution of the finally block is dependent on how the exception unwind operation is triggered. That, in turn, is dependent on how your computer is set up. For more information, see Unhandled Exception Processing in the CLR.
Ref: Try-Finally
Problem
I have the following code snippet / example. It's not working code I just wrote this so as to ask a question about catch, finally and return: ``` try { doSomething(); } catch (Exception e) { log(e); return Content("There was an exception"); } finally { Stopwatch.Stop(); } if (vm.Detail.Any()) { return PartialView("QuestionDetails", vm); } else { return Content("No records found"); } ``` From what I understand if there's an exception in the try block it will go to catch. However if there is a return statement in the catch then will the finally be executed? Is this the correct way to code a catch and finally ?