代表和例外 [英] Delegates and Exceptions

查看:72
本文介绍了代表和例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我一直在试验代表和例外。

我知道当一个单独的线程发生异常时

在调用EndInvoke时保存并检索它,但

为什么我不能重新抛出它?


问候,


示例代码:


class应用

{

delegate void MyDelegate();


static void Main(string [] args)

{

应用程序app =新应用程序();

app.Start();


Console.ReadLine();

}

public void Start()

{

MyDelegate myDelegate = new MyDelegate(BadMethod);


myDelegate.BeginInvoke(new AsyncCallback

(DelegateCallback),myDelegate);

}

public void BadMethod()

{

string newstring = string 。空;


if(newstring.Length == 0)

{

抛出新的ApplicationE xception(" delegate

测试例外);

}

}

private void DelegateCallback(IAsyncResult ar )

{

MyDelegate result =(MyDelegate)ar.AsyncState;


try

{

result.EndInvoke(ar);

}

catch

{

抛出;

}

}

}

解决方案

AC< an ******* @ discussion.microsoft.com>写道:

我一直在试验代理和异常。
我知道当一个单独的线程发生异常时,它会在调用EndInvoke时被保存和检索,但是<为什么我不能重新抛出它?




你可以 - 你认为你不能做什么?


请注意,在您的示例中,当您重新抛出异常时,没有任何内容报告它,因为它在线程池线程中没有任何额外的

异常处理程序。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件


Jon Jon,

感谢您的回复。我如何从线程池中捕获异常

?我已经尝试为AppDomain.UnhandledException注册一个处理程序

无济于事。


谢谢

-----原始消息-----
AC< an ******* @ discussion.microsoft.com>写道:

我一直在试验代表和
异常。我知道当一个单独的
线程发生异常时,它会在调用EndInvoke时保存并检索,
但为什么我不能重新抛出它?
你可以 - 让你思考的是什么你不能吗?

请注意,在你的例子中,当你的异常是



rethrown时,没有任何报告,因为它在线程池线程中没有
任何额外的处理程序。

- Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复该群组,请不要邮寄我也是





" AC" <一个******* @ discussions.microsoft.com>在消息中写道

news:11 **************************** @ phx.gbl ... < blockquote class =post_quotes>嗨Jon,

感谢您的回复。我如何从线程池中捕获异常
?我已经尝试为AppDomain.UnhandledException注册一个处理程序
无效。



线程池本身捕获并吞下它上面抛出的所有异常

个线程,所以你不会得到一个未处理的异常。你需要捕获所有

你的线程池回调例程抛出的异常然后

使用你自己的机制传播那个异常。你可以排队,

立即报告,等等。


Hi All,

I have been experimenting with delegates and exceptions.
I know that when an exception occurs on a separate thread
it is saved and retrieved when EndInvoke is called, but
why can''t I rethrow it?

Regards,

Example Code:

class Application
{
delegate void MyDelegate();

static void Main(string[] args)
{
Application app = new Application();
app.Start();

Console.ReadLine();
}
public void Start()
{
MyDelegate myDelegate = new MyDelegate(BadMethod);

myDelegate.BeginInvoke(new AsyncCallback
(DelegateCallback), myDelegate);
}
public void BadMethod()
{
string newstring = string.Empty;

if (newstring.Length == 0)
{
throw new ApplicationException("delegate
testing exception");
}
}
private void DelegateCallback(IAsyncResult ar)
{
MyDelegate result = (MyDelegate) ar.AsyncState;

try
{
result.EndInvoke(ar);
}
catch
{
throw;
}
}
}

解决方案

AC <an*******@discussions.microsoft.com> wrote:

I have been experimenting with delegates and exceptions.
I know that when an exception occurs on a separate thread
it is saved and retrieved when EndInvoke is called, but
why can''t I rethrow it?



You can - what makes you think you can''t?

Note that in your example, when your exception is rethrown, nothing is
reporting it because it''s in a threadpool thread without any extra
exception handlers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Hi Jon,
Thanks for the reply. How would I catch exceptions
from the thread pool? I''ve tried registering a handler
for the AppDomain.UnhandledException to no avail.

Thanks

-----Original Message-----
AC <an*******@discussions.microsoft.com> wrote:

I have been experimenting with delegates and exceptions. I know that when an exception occurs on a separate thread it is saved and retrieved when EndInvoke is called, but why can''t I rethrow it?
You can - what makes you think you can''t?

Note that in your example, when your exception is


rethrown, nothing isreporting it because it''s in a threadpool thread without any extraexception handlers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.




"AC" <an*******@discussions.microsoft.com> wrote in message
news:11****************************@phx.gbl...

Hi Jon,
Thanks for the reply. How would I catch exceptions
from the thread pool? I''ve tried registering a handler
for the AppDomain.UnhandledException to no avail.


The threadpool itself catches and swallows all exceptions thrown on its
threads, so you will not get an unhandled exception. You need to catch all
the exceptions thrown in your threadpool callback routine and then
propagate that exception using your own mechanism. You can queue it up,
report it immediately, etc.


这篇关于代表和例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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