Monitor.Enter可以引发异常吗? [英] Can Monitor.Enter throw an exception?

查看:154
本文介绍了Monitor.Enter可以引发异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以Monitor.Enter引发任何异常.我正在进行代码审查,并发现Monitor.Enter在try块之前.您是否发现in有任何问题?

Can Monitor.Enter throw any exception. I am doing a code review and find that Monitor.Enter is before try block. Do you see any issues with in?

Monitor.Enter(...)
try
{
    ...
}
finally
{
    Monitor.Exit(..)
}

推荐答案

这是正确的模式,无论Enter()是否抛出(可以抛出).

This is the correct pattern, whether Enter() throws (can throw) or not.

仅在调用Enter()成功之后,您的代码才有责任调用Exit().

Only after the call to Enter() succeeds your code is under the responsibility to call Exit().

假设对Enter()的调用失败.然后,调用相应的Exit()是完全错误的,这会使情况变得更糟.因此,Enter()必须在try块之前(之前).

Suppose the call to Enter() fails. Then calling the corresponding Exit() is simply incorrect, it will make matters worse. So Enter() has to be outside (before) the try block.

这篇关于Monitor.Enter可以引发异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆