Search code examples
c#visual-studiowindows-phonewin-universal-app

What is XAML generated break on unhandled exception and app.g.i.cs file


I am new to windows app development. I am trying to execute a solution on my local machine using x64 platform. But whenever I execute a Buttom_Click event I am getting this exception

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
            UnhandledException += (sender, e) =>
            {
                if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
            };
#endif

in App.g.i.cs file.

I am getting this exception when debugger hits the variable 'icon' below

 private async void Button_Click(object sender, RoutedEventArgs e)
        {
            RootObject myWeather =
                await OpenWeatherMapProxy.GetWeather(20.0,30.0);

            string icon = String.Format("ms-appx:///Assets/Weather/{0}.png", myWeather.weather[0].icon);
            ResultImage.Source = new BitmapImage(new Uri(icon, UriKind.Absolute));
            ResultTextBlock.Text = myWeather.name + " - " + ((int)myWeather.main.temp).ToString() + " - " + myWeather.weather[0].description;
        }

It would be helpful if anyone can explain how to get rid of this exception and what is App.g.i.cs file.


Solution

  • App.g.i.cs is an auto genearated file, and its breaking at that location because you haven't handled the exception properly in your code.

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        try{
    
        RootObject myWeather =
            await OpenWeatherMapProxy.GetWeather(20.0,30.0);
    
        string icon = String.Format("ms-appx:///Assets/Weather/{0}.png", myWeather.weather[0].icon);
        ResultImage.Source = new BitmapImage(new Uri(icon, UriKind.Absolute));
        ResultTextBlock.Text = myWeather.name + " - " + ((int)myWeather.main.temp).ToString() + " - " + myWeather.weather[0].description;
    
        }
        catch(Exception ex)
        {
           Debug.WriteLine(ex.Message);
           Debug.WriteLine(ex.StackTrace);
        }
    }
    

    Go to the Output window when the application is running and go through the exception detail, you might find the answer.

    Mostlikely exception is causeded by myWeather or myWeather.weather[0] being null because OpenWeatherMapProxy.GetWeather failed to fetch the data.