我在这里看到无数人谈论从异步功能捕获异常的方法,但对我没有任何作用。
我用了一些代码制作了一个IRC机器人:https : //github.com/BenFradet/RiotSharp(因为继承自该异常WebException
,如果要测试,应该做同样的事情)
所以我尝试了异步功能。GetSummoner()
例如,当RiotApi的功能找不到用户时,将抛出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不会捕获异常。
我尝试了在这里看到的所有内容:捕获异步void方法引发的异常
我也尝试了ContinueWith TaskContinuationOptions.OnlyOnFaulted
,也没有用。
我使用VS 2013和框架4.5ish
我相当迷失了atm,用尽了我在这里找到的所有东西,但没有任何效果,也许有人能想到些什么?
尝试类似:
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));
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句