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

标签 c# exception

我的程序在 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/

相关文章:

c# - Monogame - iOS 上的每像素碰撞

java - 如何在抛出异常时回滚 Spring Transaction

c# - 如何获取实现接口(interface)的类列表?

c# - .net 中的自定义属性和异常

Java:在这种特殊情况下如何处理ConcurrentModificationException?

java - 创建新阶段时抛出 ExceptionInInitializerError

即使键在字典中,Python 也会引发 KeyError(对于字典键外的键)

c# - 如何在 C# 中将 t-sql 的 "tinyint"转换为整数?

c# - 命名空间 'Interop' 在命名空间 "Microsoft.Office"中不存在

c# - 在 ASP.NET 页面上显示的 SQL <二进制数据> 图像类型