Krumelur
Krumelur

Reputation: 33068

How to catch Exception within a Task in Xamarin.iOS / Mono?

Xamarin.iOS 6.2.1, Xamarin Studio 4

I have this piece of code in ViewDidAppear():

this.refreshTask = Task.Factory.StartNew(() => this.RefreshContent());

try
{
    this.refreshTask.Wait();
}
catch(AggregateException aggEx)
{
    aggEx.Handle(x => false);
}

The method RefreshContent() causes an IndexOutOfBoundsException while accessing an array. I can see this if I run the method directly. If I run it in the Task, as above, the application does not fail and I end up with an empty tableview. According to this article: http://msdn.microsoft.com/en-us/library/dd537614.aspx the code above should handle the exception. However, the AggregateException is never triggered.

What am I doing wrong here? Or is it a bug in Xamarin.iOS / Mono?

Upvotes: 4

Views: 2739

Answers (1)

Theos
Theos

Reputation: 666

Have you tried to include a try-catch inside the new task delegate? Like this:

 Task.Factory.StartNew(() => {
        try {
             this.RefreshContent()
        } catch (Exception ex) {
             // Handle the exception
        }
  });

Upvotes: 2

Related Questions