Practically the entire point of the "lock" statement is to ensure that
the lock is _not_ held in the event of _any_ early exit from the method,
including due to an exception.
Otherwise, it wouldn't be any different than:
Monitor.Enter(something);
// some code
Monitor.Exit(something)
…which is not all that useful.
In fact, the "lock" statement is essentially a short-hand way of writing
this:
object o = something;
Monitor.Enter(o);
try
{
// some code
}
finally
{
Monitor.Exit(o);
}
So, no…do not worry at all about throwing or any other kind of early
return from the method from within a "lock" statement. It works fine.
See also the "using" statement, which provides similar reliability for
dealing with objects that implement IDisposable.
Pete