【发布时间】:2017-06-18 04:34:35
【问题描述】:
我认为我的理解中缺少一些东西。如果我在 Console.WriteLine 完成的地方设置断点,它不会停止。
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;
}
};
}
但是如果我将我的代码更改为这样,通过删除异步行为,就会触发断点:
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()).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;
}
};
}
【问题讨论】:
标签: c# .net exception-handling async-await