Link to home
Start Free TrialLog in
Avatar of mac-will
mac-willFlag for Canada

asked on

C# - try-catch (with return) in lock

Hello,

I'm looking at the IL of a function similar to this and it looks like when an exception occurs the lock is not released.

Is this correct?

Can anybody elaborate?

P.S. I know there are other ways of doing this but I want to know why the lock is not being released. (or at least doesn't seem to be being released)


int TestFunc()
{
lock(object)
{
try
{
...
return 1;
}
catch(Exception)
{
return -1;
}
}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Daniel Reynolds
Daniel Reynolds
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mac-will

ASKER

Thanks people.
However I know how it is suppose to work but I wasn't getting the expected behaviour.

I found this and I think this may be what I am seeing.

http://blogs.msdn.com/b/ericlippert/archive/2009/03/06/locks-and-exceptions-do-not-mix.aspx