Albahari Threading section about ReaderWriterLockSlim shows usage of this locker. But there is no any try-catch and finally blocks against exceptional cases.
Why he hasn't used exception handling to not leave opened locks ? At below example, write lock left opened if DivideBy parameter is 0.
So he tries simply show usage for not to make example complicated? If so what is best practice to handle this problem ? We should to always use finally block for exit lock if it is held ?
public class Divide
{
static int x = 1000;
static ReaderWriterLockSlim locker = new ReaderWriterLockSlim();
public void DivideBy(int divide)
{
locker.EnterWriteLock();
x = x / divide;
locker.ExitWriteLock();
}
}