为什么异常不被抓住? [英] Why is the exception not being caught?

查看:170
本文介绍了为什么异常不被抓住?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为在我的理解中缺少一些东西。如果我把一个断点放在$ code> Console.WriteLine 完成的catch中,它不会停止。

  private static void Main(string [] args)
{
进程(async()=&a)等待ClientMethod())。
Console.Read();
}

public static async Task ClientMethod()
{
throw new Exception(Test);
}

public static Action Process(Action functor)
{
return()=>
{
try
{
functor();
}
catch(异常)
{
//处理异常?
Console.WriteLine(In the catch);
throw;
}
};
}

但是,如果我将代码更改为这个,通过删除异步行为,断点被击中:

  private static void Main(string [] args)
{
Process(( )=> ClientMethod())。Invoke();
Console.Read();
}

public static void ClientMethod()
{
throw new Exception(Test);
}

为什么在第一种情况下没有捕获到异常?我如何抓住它?



编辑:我将代码改成了这个,但是仍然是一样的:

  private static void Main(string [] args)
{
var res = Process(async()=> await ClientMethod ))调用();
Console.Read();
}

public static async任务< string> ClientMethod()
{
throw new Exception(Test);
}

public static Func< T>处理< T>(Func< T>函子)
{
return()=>
{
try
{
return functor();
}
catch(异常)
{
//处理异常?
Console.WriteLine(In the catch);
throw;
}
};
}


解决方案

被调用为 async void



引用:从异步/等待 - 异步编程的最佳实践


Async void方法具有不同的错误处理语义。当一个
异常从异步任务异步任务< T> 方法抛出时,该
异常被捕获并置于Task对象上。使用async void
方法,没有Task对象,所以从
async void方法抛出的任何异常将直接在
SynchronizationContext 在async void方法
启动时处于活动状态。



I think there is something missing in my understanding. If I put a breakpoint in the catch where the Console.WriteLine is done, it does not stop.

private static void Main(string[] args)
{
    Process(async () => await ClientMethod()).Invoke();
    Console.Read();
}

public static async Task ClientMethod()
{
    throw new Exception("Test");
}

public static Action Process(Action functor)
{
    return () =>
    {
        try
        {
            functor();
        }
        catch (Exception)
        {
            // Handle exceptions ?
            Console.WriteLine("In the catch");
            throw;
        }
    };
}

But if I change my code into this, by removing the async behavior, the breakpoint is hit:

private static void Main(string[] args)
{
    Process(() => ClientMethod()).Invoke();
    Console.Read();
}

public static void ClientMethod()
{
    throw new Exception("Test");
}

Why the exception is not caught in the first case? How can I catch it?

EDIT: I changed my code into this but it is still the same:

private static void Main(string[] args)
{
    var res = Process(async () => await ClientMethod()).Invoke();
    Console.Read();
}

public static async Task<string> ClientMethod()
{
    throw new Exception("Test");
}

public static Func<T> Process<T>(Func<T> functor)
{
    return () =>
    {
        try
        {
            return functor();
        }
        catch (Exception)
        {
            // Handle exceptions ?
            Console.WriteLine("In the catch");
            throw;
        }
    };
}

解决方案

Because the action in Process is being invoked as an async void.

Quote: from Async/Await - Best Practices in Asynchronous Programming

Async void methods have different error-handling semantics. When an exception is thrown out of an async Task or async Task<T> method, that exception is captured and placed on the Task object. With async void methods, there is no Task object, so any exceptions thrown out of an async void method will be raised directly on the SynchronizationContext that was active when the async void method started.

这篇关于为什么异常不被抓住?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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