异常处理中的错误 [英] Bug in exception handling

查看:71
本文介绍了异常处理中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么向Microsoft报告错误是如此困难?我有一个记录的错误

和一个小的测试示例。我不知道为什么我要付钱给

告诉他们这个...


无论如何,下面的代码展示了这个bug。如果它是在调试模式下从

Visual Studio.NET 2003构建和运行的,它可以按预期正常工作。

但是,如果构建在Release模式下,它将失败并显示未捕获的异常

错误,虽然异常确实有一个有效的catch处理程序。

要重复,请创建一个新的Windows窗体应用程序并输入以下代码<在Form1.cs中获得



----


///< summary>

///应用程序的主要入口点。

///< / summary>

[STAThread]

static void Main()

{

try

{

Application.Run(new Form1()) ;

}

catch(例外情况)

{

MessageBox.Show(ex.Message,"主要:抓住异常:);

}

}


private void button1_Click(对象发送者,System.EventArgs e )

{

试试

{

Foo();

}

catch(Exception ex)

{

MessageBox.Show(ex.Message," button1_Click:Caught exception:");

throw;

}

}


void Foo()

{

抛出新的异常(从Foo()抛出异常);

}

解决方案

嗨David,


我不能说它是否在Service Pack 1中得到修复,但代码在Framework 2.0中运行良好,包括调试和发布。


-

快乐编码!

Morten Wennevik [C#MVP]


> Application.Run(new Form1());


而不是WinForms的设计,外部抛出异常

Application.Run没有被捕获通过try-catch但由线程'的全局

异常处理程序由Application.ThreadException设置。


所以,而不是


试试

{

Application.Run(...)

}

catch (){...}


使用


Application.ThreadException + = new ThreadExceptionEventHandler(

MyExceptionHandler) ;

Application.Run(...);


....


public static void MyExceptionHandler (对象发件人,

ThreadExceptionEventArgs e)


{


Exception ex = e.Exception;


//做一些异常但你不太可能恢复

申请


Application.Exit();


}


问候,


Wiktor Zychla


嗨David,


< snip>

为什么向Microsoft报告错误是如此困难?我有一个记录错误的
和一个小的测试示例。我不明白为什么我要付钱给
告诉他们... ... b $ b

是啊!

这似乎是是M

中的真正问题

Why is it so difficult to report bugs to Microsoft? I have a documented bug
and an small test example. I don''t really see why I should have to pay to
tell them about it...

Anyway, the following code exhibits the bug. If it is built and run from
Visual Studio.NET 2003 in debug mode it works correctly as expected.
However, if built in Release mode it fails with an "uncaught exception"
error, although the exception does have a valid catch handler.

To repeat, create a new Windows Forms application and put the following code
into Form1.cs.

----

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.Run(new Form1());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Main: Caught exception: ");
}
}

private void button1_Click(object sender, System.EventArgs e)
{
try
{
Foo();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "button1_Click: Caught exception: ");
throw;
}
}

void Foo()
{
throw new Exception("Exception thrown from Foo()");
}

解决方案

Hi David,

I can''t say if it is fixed in service pack 1, but the code runs fine in Framework 2.0 both debug and release.

--
Happy Coding!
Morten Wennevik [C# MVP]


> Application.Run(new Form1());

it is rather by design of the WinForms that the exceptions thrown outside
the Application.Run are not caught by try-catch but by the thread''s global
exception handler set by Application.ThreadException.

so, instead of

try
{
Application.Run(...)
}
catch() {...}

use

Application.ThreadException += new ThreadExceptionEventHandler(
MyExceptionHandler );
Application.Run(...);

....

public static void MyExceptionHandler( object sender,
ThreadExceptionEventArgs e )

{

Exception ex = e.Exception;

// do something with the exception but you are unlikely to resume the
application

Application.Exit();

}

regards,

Wiktor Zychla


Hi David,

<snip>

Why is it so difficult to report bugs to Microsoft? I have a documented bug
and an small test example. I don''t really see why I should have to pay to
tell them about it...
Yeah!
That seems to be real problem in M


这篇关于异常处理中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆