【发布时间】:2019-11-02 09:07:51
【问题描述】:
我正在努力解决仅在 Windows 7 的调试模式下出现的奇怪问题。
我试图捕捉我在方法 delcared async 中抛出的异常。是的,我正在等待它,但异常仍然存在于内部函数中。请参阅下面的代码。
- 在应用配置中设置了所需的框架 (4.6.1) 版本。
- 尝试设置 C# 语言的严格版本。
- 在 Release 或 Windows 10 中运行良好。
- 仅在 VS 2017 中测试。
private void Connect(int port)
{
lock (apiLocker)
{
if (port <= 0)
{
throw new ArgumentException("Invalid port number");
}
//etc.
}
}
internal async Task ConnectAsync(int port)
{
await Task.Run(() =>
{
Connect(port);
});
}
调用看起来像这样:
private async Task<bool> ConnectAsync()
{
try
{
await RadioDispatcher.Instance.ConnectAsync(connectionSettings.Port);
return true;
}
catch
{
//I can't reach that line in Debug on windows 7.
//In Release on Windows 10 it works fine.
return false;
}
}
【问题讨论】:
-
有什么例外?即使您不处理它,调试也总是会告诉您代码中出了什么问题。
标签: c# asynchronous exception