Solved

MvvmCross Xamarin Error Handling

Posted on 2016-09-20
2
23 Views
Last Modified: 2016-11-05
I need help concerning error handling in MvvmCross Xamarin framework. How can I catch errors from async methods in the ViewModels.
0
Comment
Question by:Bassel Chaitani
2 Comments
 
LVL 8

Accepted Solution

by:
Paweł earned 500 total points
ID: 41806862
your async function can't be void, it has to return a type so that you can await it

void Void_BTN_Click(object sender, RoutedEventArgs e)
{
    var n = Convert.ToInt32(this.Numerator_TXT.Text);
    var d = Convert.ToInt32(this.Denominator_TXT.Text);

    try
    {
        //Cant await
        CalculateAsync1(n, d);
    }
    catch (DivideByZeroException ex)
    {
        this.Result_TXT.Text = "Void undefined";
    }
}

async void CalculateAsync1(int n, int d)
{
    this.Result_TXT.Text = (n / d).ToString();
}

Open in new window


because our asynchronous void method cannot be awaited its exceptions cannot be caught however it doesn't just silently fail it'll crash your application, hence it's best that your asynchronous functions return tasks.

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //Can await
                output_TXT.Text = await CalculateAsync(3,0);
            }
            catch (DivideByZeroException ex)
            {
                output_TXT.Text = ex.Message;
            }
        }

        private Task<string> CalculateAsync(int v1, int v2)
        {
            return Task.Factory.StartNew(()=>(v1 / v2).ToString());
        }

Open in new window


basically the same thing but since we return a task we can catch the exception.

not sure if it'll function the same way in xamarin, but i'd think it should
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now