Solved

MvvmCross Xamarin Error Handling

Posted on 2016-09-20
2
12 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

18 Experts available now in Live!

Get 1:1 Help Now