C# try...catch 语句在异步操作中发生异常时不捕获异常

发布于 2024-12-11 00:05:10 字数 856 浏览 0 评论 0原文

例如:

第一步:绑定UnhandledException事件。

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

第二步:

try
    {
       //at here,do asynchronous operation and throw a exception.
    }
catch (Exception ex)
   {
            Console.WriteLine("error");
   }

当异步操作抛出异常时,它不会调用catch代码,只是触发UnhandledException事件,在调用end事件后退出应用程序。

我想捕获 catch 语句中的任何异常并避免退出应用程序。

=================================================== =====

异步代码是异步socket操作。在socket异步接收消息事件(BeginReceive,EndReceive)中,我抛出了OverFlowException。

throw new OverflowException("chunk size too long.");

===============================================

你是对的,现在我捕获异步操作中的异常并将其传递给原始类(这意味着异常将在同一线程上抛出,可以尝试...可以调用catcy语句)

for example:

first step:bind UnhandledException event.

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

second step:

try
    {
       //at here,do asynchronous operation and throw a exception.
    }
catch (Exception ex)
   {
            Console.WriteLine("error");
   }

when the asynchronous operation throw a exception,the catch code it not called,just only the UnhandledException event is triggered,after end event called then exit application.

i want capture exception anything in catch statement and avoid exit application.

=======================================================

the asynchronous code is the asynchronous socket operation.in the socket asynchronous receive message event(BeginReceive,EndReceive),i throw a OverFlowException.

throw new OverflowException("chunk size too long.");

=============================================

you are right,now i capture the exception in asynchronous operation and passed it to original class(this means exception will throw on the same thread,that can try...catcy statement can was called)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很酷又爱笑 2024-12-18 00:05:10

默认情况下,异步任务将在与其实例化的上下文不同的上下文中运行。因此,try/catch 块在这种情况下无效。

请按以下方式思考。

Worker worker = new HouseMaid();
try
{
    worker.DoSomeWork();
}
catch(WorkerIsSickException ex)
{
    worker.TakeMedicin();
    worker.StopWorkingAndRestForAWhile();
}

这里我们看到,当工人生病时,工作流程就会中断,异常就会被处理。但是,当我们这样做时:

Worker worker = new HouseMaid();
try
{
    Worker otherWorker = new HouseMaidHelper();

    worker.DelegateWorkTo(otherWorker, CallBackWhenOTherWorkerIsDone);

    worker.GoOnDoSomethingElse();
}
catch(WorkerIsSickException ex)
{
    worker.TakeMedicin();
    worker.StopWorkingAndRestForAWhile();
}

工作 try/catch 块(或安全网,如果您愿意的话)将仅适用于该工作人员,而不适用于其他工作人员。 otherWorker有自己的工作范围。如果其他Worker失败了,那不应该意味着该Worker必须吃药。

An asynchronous task by default will operate in a different context from the one it was instantiated in. The try/catch block is therefore not effective in this case.

Think about it in the following way.

Worker worker = new HouseMaid();
try
{
    worker.DoSomeWork();
}
catch(WorkerIsSickException ex)
{
    worker.TakeMedicin();
    worker.StopWorkingAndRestForAWhile();
}

Here we see that when the worker gets sick, the work process breaks and the exception will be handeled. However, when we do:

Worker worker = new HouseMaid();
try
{
    Worker otherWorker = new HouseMaidHelper();

    worker.DelegateWorkTo(otherWorker, CallBackWhenOTherWorkerIsDone);

    worker.GoOnDoSomethingElse();
}
catch(WorkerIsSickException ex)
{
    worker.TakeMedicin();
    worker.StopWorkingAndRestForAWhile();
}

The work try/catch block (or safety net if you will), will only apply for the worker, not otherWorker. The otherWorker has its own scope to work in. If the other worker fails, that shouldnt mean that the worker has to take the medicine.

诠释孤独 2024-12-18 00:05:10

您的问题不清楚,但如果是引发异常的异步操作,那么您所显示的代码无论如何都可以在引发异常之前轻松完成 - 这都是异步的,对吗?

基本上,异步让您重新思考错误处理 - 而如何做到这一点取决于您的异步方法。您经常会在回调中捕获异常 - 或者甚至捕获它,但使用诸如Task.Status之类的东西检查它。

诚然,C# 5 中针对异步内容所做的所有工作应该会让这一切变得更容易,但您仍然需要考虑异步性。

Your question isn't clear, but if it's the asynchronous operation which throws the exception, then the code you've shown could easily have completed before the exception is thrown anyway - it's all asynchronous, right?

Basically asynchrony makes you rethink error handling - and quite how you do it depends on your asynchrony approach. You'd often catch the exception in the callback - or not even catch it, but check for it with something like Task.Status.

Admittedly all the work in C# 5 for async stuff should make some of this easier, but you still need to think about the asynchrony.

<逆流佳人身旁 2024-12-18 00:05:10

异步操作将成功启动,因此执行将成功继续并错过异常处理程序。如果要捕获异步操作中的异常,则需要在异步执行的代码中进行异常处理。如果您想处理那里的异常,您可以对原始类中的函数进行回调。

The asynchronous operation will get started successfully, hence execution will continue successfully and miss your exception handler. If you want to catch the exception within the asynchronous operation, you will need exception handling in the code that is executing asynchronously. You could then put a call back to a function in your original class if you want to handle the exception there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文