2

I saw here numerous people talking of ways to catch exception from async fonctions, yet nothing worked from me.

I used some code to make an IRC bot : https://github.com/BenFradet/RiotSharp (since the exception is inherited from WebException it should do the same if you want to test)

So i tried async functions. When the user is not found by a function from RiotApi, GetSummoner() for example, a 404 is thrown as 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));
}

I have used that numerous time and i can catch that 404. Yet if i use the async version : wether i go for

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));
}

or for Task and then away, the try/catch won't catch the exception.

I tried everything i saw here : Catch an exception thrown by an async void method

I tried too the ContinueWith TaskContinuationOptions.OnlyOnFaulted which did not work either.

I use VS 2013 and framework 4.5ish

I am fairly lost atm and used all i found here and nothing worked, maybe someone can think of something ?!

shamp00
  • 11,106
  • 4
  • 38
  • 81
Tensort
  • 39
  • 2
  • 2
    Exception would not be caught in the Async version until and unless you try to dosomething with the returned Task object, infact, Exception property of the Task should provide the AggregateException, which has innerexception, which would contain specific Exception you need. Use any Task object method like Result and catch will get the Exception. This is the behavior of TPL. – Mrinal Kamboj May 07 '15 at 08:15
  • 2
    The async version you include in your question (i.e. where you `await` the operation) should throw an exception just as the synchronous version would. If it doesn't, that's a bug in the RiotApi component you're using. Without [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) I don't see a way to comment any more specifically than that, never mind answer the question. – Peter Duniho May 07 '15 at 08:23
  • 1
    Also can you catch a Generic Exception or Aggregated Exception and check, since Async call will throw an aggregated exception, not the Riot exception you are expecting, as suggested earlier, you have get your exception out of it – Mrinal Kamboj May 07 '15 at 08:53
  • I donwloaded RC 2105 of VS, and the same code just worked fine ... I don't understand :( – Tensort May 07 '15 at 08:55
  • Where is the code that actually raises this exception? Are you sure you are raising `RiotSharpException ` instead of `AggregateException`? Did you try catching `Exception` to see what is actually thrown? Exception handling with tasks works or people would have noticed that such a major feature is broken. – Panagiotis Kanavos May 07 '15 at 09:09
  • yeah i tried Exception too to get the lower possible. The stack trace gave me the exception i was expecting, but i did not catch it. As i said a bit earlier, the very same code is working just fin on VS2015. And the code you all gave : Not working on VS2013 I load it in 2015, it works just fine. i don't understand why and i'll stuck to the working one i guess ... – Tensort May 07 '15 at 09:20
  • 1
    VS 13 works. Async exception handling works in hundreds of thousand of production deployments. VS 15 is not ready yet. Instead of assuming that something so major is broken post *complete* and *reproducible* code *and the complete results*. The stack trace may contain the exception 2 levels down in an AggregateException but noone can guess – Panagiotis Kanavos May 07 '15 at 09:27
  • What you are pointing out suggests that in VS 2015, .Net 4.6 and C# 6.0 there's little more intelligence in exception handling if the exception thrown internally contains just one exception like RiotException out here, then it internally converts and shows the result, but that's not possible till now (VS 2013), which needs a workaround as suggested in the answer – Mrinal Kamboj May 07 '15 at 12:24

2 Answers2

1

Try something like:

               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));
                  } 

                 }
Mrinal Kamboj
  • 11,300
  • 5
  • 40
  • 74
  • i already tried since i have read something like 5 similar errors here. It does not work, yet it works in 4.6 framework and VS 15. I read somewhere that this was not supported by VS 13 and i am starting to belive that ... But thanks for the help ! – Tensort May 07 '15 at 09:11
  • 1
    Actually it works very well in production in hundreds of thousands of deployments. You really need to post code that reproduces the error - including any unhandled exception handlers. It may simply be that the project you are using has issues – Panagiotis Kanavos May 07 '15 at 09:24
  • I can send you the whole VS project if you want to look at it. Yet it works not that i used VS2015 (i loaded the exact project in 2015 and it worked first time) And now it works with 2013 when i switch back. Either i am a big fool (most probably .... he, most errors come from between chair and keyboard ) Either i encountered a weird bug – Tensort May 07 '15 at 09:39
0

Well it seems to work just well on 2015 RC with 4.6 framework ... so i'll stuck to that

Tensort
  • 39
  • 2
  • There is nothing broken with exception handling in async methods. Most likely `GetSummonerAsync` methods throw a different exception than the one you think. – Panagiotis Kanavos May 07 '15 at 09:15
  • the unhandled exception was the one i was expecting (i tried with Exception too that is the root and should work) And i tried the EXACT same code with VS 2015 and it catches just well. i am speechless :( all your solutions work well but on VS15 – Tensort May 07 '15 at 09:18