简体   繁体   English

为什么抛出的异常与catch块C#没有正确匹配

[英]Why thrown exception is not correctly matched with catch block C#

Facing some weird issue with SocketException. 面对SocketException的一些奇怪问题。 Attached screenshot speaks more about it. 随附的屏幕截图更多地讲述了它。

在此输入图像描述

  1. 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。

  2. 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.

相关问题 为什么这个catch块没有捕获抛出的异常? - Why is this catch block not catching the thrown exception? 在C#中传播在finally块中抛出的异常而不丢失catch块中的异常的最佳实践是什么? - What is the best practice in C# to propagate an exception thrown in a finally block without losing an exception from a catch block? 在C#中,如果关联的catch块引发异常,是否可以强制控制通过finally块? - in C# is it possible to force control to pass through a finally block if an exception is thrown by an associated catch block? C# 异常处理 - 上次捕获块获取是否会重新抛出异常? - C# Exception Handling - will last catch block fetch re-thrown Exception? Selenium C# (NUnit) - 异常未在 catch 块中捕获,因为即使抛出异常也会执行 [onetimeteardown] - Selenium C# (NUnit)- Exception not caught in catch block as [onetimeteardown] executes even if exception thrown C# 为什么在 try-catch 块内引发异常? - C# Why was exception raised inside try-catch block? 在C#的Try块中,如果要在我的textbock中输入一个int,我想引发异常的catch部分吗? - In C# a Try block, the catch section i want a exception to be thrown if in my textbock a int is entered? 如何在catch块c#中管理异常? - How to manage exception in catch block c#? 从C#中的Delphi DLL抛出异常 - Catch exception thrown from Delphi DLL in C# Try/catch 没有得到 c# 中 Assert 抛出的内部异常 - Try/catch is not getting the inner exception thrown by Assert in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM