This is just a bug. You need to guarantee that the mutex always released, so you should not release it under the "try" block, you should not release it under "catch" block; you should only release it in the "finally" block.
Are you saying that it all "works" properly without correctly releasing the mutex? There can be two explanations:
- Mutex does not do anything useful; in other words, you don't really have any shared resources protected by the mutex.
- Mutex (more exactly, not mutex, but some kind of mutual exclusion; see below) is really needed, but, just by a chance, your timing never got you to the situation where the shared resource is actually accessed non-exclusively. That would be the worse-case scenario. Your code could work "correctly" for months or years and got crashed somewhere during production.
In other words, you could have created race condition, incorrect dependency on the time of execution, which is a deadly thing. Please see and understand thoroughly: http://en.wikipedia.org/wiki/Race_condition[^].
Whatever it is, but the code is wrong and dangerous. Do you need to use
finally
block instead? It depends.
First of all, it's likely that you don't need mutex. Is the mutex named? If not, you most certainly does not need it. If you don't have different processes working with the same shared resource, only then you may or may not need a mutex. In other cases, instead, you can use more lightweight
lock
statement:
https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[
^].
Besides, it could be possible that you don't have mutual exclusion at all. Do you even have several threads? With ASP.NET, this is pretty unlikely. You need to understand when it is not needed and when not. Only remember: "the best synchronization is no synchronization". Please read this and understand thoroughly:
https://en.wikipedia.org/wiki/Mutual_exclusion[
^].
—SA