gpt4 book ai didi

c# - Try/Catch block 无法捕获异常

转载 作者:行者123 更新时间:2023-11-30 15:26:58 26 4
gpt4 key购买 nike

我的程序在 try/catch block 中生成 System.ComponentModel.Win32Exception,但未捕获该异常。代码非常简单:

try
{
lAFE.MinimumSize = sz1; // lAFE=Label, sz1 = Size
}
catch (Exception ex)
{
MessageBox.Show("afe: " + ex.Message);
}

程序在没有问题的情况下运行此代码块数百次,然后突然生成此异常并且未被捕获。

什么会导致像这样的异常没有被捕获。

此应用程序占用大量内存,当内存占用达到305KB左右时,总是出现异常。

如有任何建议,我们将不胜感激。

最佳答案

因为 Win32 异常不是从 .NET Exception 类派生的。尝试:

try 
{
}
catch (Exception ex)
{
// .NET exception
}
catch
{
// native exception
}

你可以阅读这个 article :

A catch block that handles Exception catches all Common Language Specification (CLS) compliant exceptions. However, it does not catch non-CLS compliant exceptions. Non-CLS compliant exceptions can be thrown from native code or from managed code that was generated by the Microsoft intermediate language (MSIL) Assembler. Notice that the C# and Visual Basic compilers do not allow non-CLS compliant exceptions to be thrown and Visual Basic does not catch non-CLS compliant exceptions. If the intent of the catch block is to handle all exceptions, use the following general catch block syntax.

C#: catch {}

关于c# - Try/Catch block 无法捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28226301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com