C# 等待并捕获异常
C# await and catch exception
我在这里看到很多人在谈论如何从异步函数中捕获异常,但我没有任何效果。
我使用一些代码制作了一个 IRC 机器人:https://github.com/BenFradet/RiotSharp
(因为异常是从 WebException
继承的,如果你想测试它应该做同样的事情)
所以我尝试了异步函数。当 RiotApi 的函数找不到用户时,例如 GetSummoner()
,将抛出 404 作为 RiotSharpException
.
try
{
RiotSharp.SummonerEndpoint.Summoner summoner = api.GetSummoner(region,name);
SendSummonersDetails(summoner, e.Message.MessageTarget);
}
catch (RiotSharpException ex)
{
string error = string.Format(this.RegionNotFound, command[2]);
irc.Send(new IRCMessage("PRIVMSG", e.Message.MessageTarget, error));
}
我已经用了很多次了,我可以捕捉到那个 404。
但是,如果我使用异步版本:
我是否会去
try
{
RiotSharp.SummonerEndpoint.Summoner summoner = await api.GetSummonerAsync(region,name);
SendSummonersDetails(summoner, e.Message.MessageTarget);
}
catch (RiotSharpException ex)
{
string error = string.Format(this.RegionNotFound, command[2]);
irc.Send(new IRCMessage("PRIVMSG", e.Message.MessageTarget, error));
}
或者对于 Task 然后离开,try/catch 不会捕获异常。
我尝试了我在这里看到的一切:
Catch an exception thrown by an async void method
我也试了 ContinueWith TaskContinuationOptions.OnlyOnFaulted
也没用。
我使用 VS 2013 和框架 4.5ish
我完全迷失了 atm 并使用了我在这里找到的所有东西,但没有任何效果,也许有人能想到点什么?!
嗯,它似乎在 2015 RC 4.6 框架上工作得很好......所以我会坚持下去
试试这样的东西:
try
{
RiotSharp.SummonerEndpoint.Summoner summoner = await api.GetSummonerAsync(region,name);
SendSummonersDetails(summoner, e.Message.MessageTarget);
}
catch (AggregateException ex)
{
foreach(Exception ee in ex.InnerExceptions)
{
RiotException e = ee as RiotException;
string error = string.Format(this.RegionNotFound, command[2]);
irc.Send(new IRCMessage("PRIVMSG", e.Message.MessageTarget, error));
}
}
我在这里看到很多人在谈论如何从异步函数中捕获异常,但我没有任何效果。
我使用一些代码制作了一个 IRC 机器人:https://github.com/BenFradet/RiotSharp
(因为异常是从 WebException
继承的,如果你想测试它应该做同样的事情)
所以我尝试了异步函数。当 RiotApi 的函数找不到用户时,例如 GetSummoner()
,将抛出 404 作为 RiotSharpException
.
try
{
RiotSharp.SummonerEndpoint.Summoner summoner = api.GetSummoner(region,name);
SendSummonersDetails(summoner, e.Message.MessageTarget);
}
catch (RiotSharpException ex)
{
string error = string.Format(this.RegionNotFound, command[2]);
irc.Send(new IRCMessage("PRIVMSG", e.Message.MessageTarget, error));
}
我已经用了很多次了,我可以捕捉到那个 404。 但是,如果我使用异步版本: 我是否会去
try
{
RiotSharp.SummonerEndpoint.Summoner summoner = await api.GetSummonerAsync(region,name);
SendSummonersDetails(summoner, e.Message.MessageTarget);
}
catch (RiotSharpException ex)
{
string error = string.Format(this.RegionNotFound, command[2]);
irc.Send(new IRCMessage("PRIVMSG", e.Message.MessageTarget, error));
}
或者对于 Task 然后离开,try/catch 不会捕获异常。
我尝试了我在这里看到的一切: Catch an exception thrown by an async void method
我也试了 ContinueWith TaskContinuationOptions.OnlyOnFaulted
也没用。
我使用 VS 2013 和框架 4.5ish
我完全迷失了 atm 并使用了我在这里找到的所有东西,但没有任何效果,也许有人能想到点什么?!
嗯,它似乎在 2015 RC 4.6 框架上工作得很好......所以我会坚持下去
试试这样的东西:
try
{
RiotSharp.SummonerEndpoint.Summoner summoner = await api.GetSummonerAsync(region,name);
SendSummonersDetails(summoner, e.Message.MessageTarget);
}
catch (AggregateException ex)
{
foreach(Exception ee in ex.InnerExceptions)
{
RiotException e = ee as RiotException;
string error = string.Format(this.RegionNotFound, command[2]);
irc.Send(new IRCMessage("PRIVMSG", e.Message.MessageTarget, error));
}
}