为什么win32异常不会被C#中的异常处理机制捕获
我有一个WinForms application.Winforms开始Program.cs中,我们主要有()defined.I已经把try-catch块这个代码。
I have a winforms application.Winforms start with Program.cs where we have main() defined.I have put this code in try-catch block.
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmSplash());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
if (ex.InnerException != null)
{
MessageBox.Show(ex.InnerException.ToString());
}
}
}
每当有win32异常,此机制失败和未处理的异常消息抛出应用程序崩溃
我有2个关于这个代码的问题:
Whenever there is a win32 exception,this mechanism fails and unhandled exception message is thrown and application crashes.
I have 2 questions regarding this code:
1)为什么Win32异常没有被卡住。
1) Why win32 exceptions are not caught.
2),它是一个很好的做法,以赶在最高水平的异常。
2) Is it a good practice to catch exceptions at the highest level.
修改:与 PRATIK 指出,下面的答案适用于.NET 1.0和.NET只有1.1。在.NET 2.0开始,非CLS异常应该被捕获为的 RuntimeWrappedException 。
EDIT : as Pratik pointed out, the following answer applies to .NET 1.0 and .NET 1.1 only. Starting with .NET 2.0, non-CLS exception should be caught as a RuntimeWrappedException.
由于Win32异常不会从派生在.NET Exception类。尝试:
Because Win32 exceptions do not derive from the .NET Exception class. Try :
try {
} catch (Exception ex) {
// .NET exception
} catch {
// native exception
}
请参阅抓住非CLSCompliant例外了解更多信息。
See Catch non-CLSCompliant exceptions in general handlers for more information.