Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MvvmCross Xamarin Error Handling

Posted on 2016-09-20
2
Medium Priority
?
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
Paweł earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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