[英]Why thrown exception is not correctly matched with catch block C#
Facing some weird issue with SocketException. 面对SocketException的一些奇怪问题。 Attached screenshot speaks more about it.
随附的屏幕截图更多地讲述了它。
Why first catch block is not catching the exception while it is meant to catch the SocketException and thrown exceptions is again a SocketException. 为什么第一个catch块没有捕获异常,而它意味着捕获SocketException而抛出的异常又是一个SocketException。
Why control is going to second catch block which is meant to catch any exception ? 为什么控制会进入第二个捕获块,这意味着捕获任何异常?
[Edit] : Adding screenshot in debug mode too [编辑]:在调试模式下添加屏幕截图
Execution is not entering the second catch block, try-catch
obviously doesn't work that way, the exception is thrown up the call stack, any following catch
clauses are ignored. 执行没有进入第二个catch块,
try-catch
显然不会那样工作,异常被抛出调用堆栈,任何后续catch
子句都被忽略。
It simply looks that way because you are debugging in Release mode which is not a good idea as the IDE is probably missing important debugging information and is therefore erroneously showing that execution halts when entering the second catch
clause. 它只是看起来那样,因为你在发布模式下调试这不是一个好主意,因为IDE可能缺少重要的调试信息,因此在输入第二个
catch
子句时错误地显示执行停止。
Do notice that halting at that point really makes no sense, if execution somehow made it into the second catch
clause it would halt at the throw
statement, not before. 请注意,在那一点停止真的没有意义,如果执行以某种方式使它成为第二个
catch
子句,它将在throw
语句中停止,而不是之前。
To make it short, what's really going on is that execution is halting on the first throw
and the IDE is not showing it correctly. 为了缩短它,真正发生的是第一次
throw
执行停止并且IDE没有正确显示它。
Moral of the story; 故事的道德启示; do not debug in Release mode.
不要在发布模式下调试。 If you are running VS2015 it actually warns you that debugging experience in Release mode will be subpar (not sure if this warning appeared in previous versions).
如果您正在运行VS2015,它实际上会警告您, 发布模式下的调试体验将会低于标准(不确定此警告是否出现在先前版本中)。
UPDATE : It seems the issue is not related to debugging in Release mode as it still happens in Debug mode. 更新 :似乎问题与发布模式下的调试无关,因为它仍然在调试模式下发生。 Still, the point stands that debugging shouldn't be done in Release mode as funky stuff like this can happen.
不过,关键是调试不应该在发布模式下进行,因为这样的时髦事情可能会发生。 I would try cleaning the solution and see if that solves the problem.
我会尝试清理解决方案,看看是否能解决问题。 If it doesn't I wouldn't sweat it, the code is behaving correctly, its just a matter of the IDE not showing correctly where the execution is halting, no big deal.
如果它没有,我不会出汗,代码表现正常,它只是IDE无法正确显示执行停止的地方,没什么大不了的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.