0
votes

assume the code is correct and webservice timeout occurs.

The problem :

  1. The system crashes and can not display the error message.

How to display error message? So I can provide an alternative to user when there is an error?


1)
 I add this  Class in the project :

 public class MyClass
 {
    public static async Task LogInSuccess()
     {
       try
        {
           -- calling a web service here 

         }
        catch (System.Exception _ex)
        {
           _strErrorMsg = _ex.InnerException.Message;
           throw new Exception("LogInSuccess() " + _strErrorMsg);

        }
     }
 }


--- In the MainPage, 
2)

 private async void SetUp ()
   {

     -- code for doing setUp task--

     CallWebSvc();

   }

3) 


private void CallWebSvc()
{
   bool ShowError = false;
   System.Exception MyException = new Exception();

       try
        {
           -- calling a web service thru the MyClass

          System.Threading.Tasks.Task _blnLogInSuccess = MyClass.LogInSuccess();
       await _blnLogInSuccess;

           if (_blnLogInSuccess.IsCompleted)
           {
              g_blnLoginStatus = _blnLogInSuccess.Result;
           }


        }
        catch (System.Exception _ex)
        {
          ShowError = true;
          MyException = ex;  

        }
       if (ShowError)
        {
         var MyMessageBox = new Windows.UI.Popups.MessageDialog("Remote Login Error:" + MyException.Message, "Start Login" );
        await MyMessageBox.ShowAsync();
        }
}


1
You are nothing in output view?MatDev8
The output view is displayed as part of the system crashed message BUT not as the MessageDialog Box as part of the App without system crashed. So, I want to display message in MessageDialog for error in the class file.MilkBottle

1 Answers

0
votes

I assume your CallWebSvc method is async void (as, without async you cannot perform an await) If this is the case, you need to know async void doesn't do the same treatament to exceptions as async task. they aren't catched correctly. If you change your CallWebSvc from async void to async Task, you are going to receive the exception correctly.