locked
Help please in exception handeling RRS feed

  • Question

  • Hello

    I need some explanation on how to handle exceptions. I know I can use  a try catch block, what I want to know is how to I catch a specific exception and show my own message. here is an example,

    try
    {
       _geolocator = new Geolocator();
       Geoposition pos = 
                  await _geolocator.GetGeopositionAsync();
    }
    catch (Exception e2)
    {
        ShowMessage.Show(e2.Message);
    
    }
              
    

    My question is how do I know what type of exceptions the Geolocator throws ? how do I show my own message to the user for each exception.

    Thank you

    Monday, September 9, 2013 11:44 PM

Answers

  • Hi Avichai,

    Try catch block can have following structure, when you disconnect the Internet, the Exception will thrown and will be caught by the last Exception catch block, there you could handle all the exception that did not handled by System.UnauthorizedAccessException and TaskCanceledException.

    And what do you mean by have your own error message? I'm not quite understand this.

    Do you want to show the user a self-defined error message? The sample code use the TextBlock/TextBox to showing the message like "NO Data".  Or you want the system throw your error message.

    try
    {
        ...
    }
    catch (ExceptionTypeA e)
    {
        ...
    }
    catch (ExceptionTypeB e)
    {
        ...
    }
    catch (Exception e)
    {
        //for the exceptions that did not caught above.
        ...
    }

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, September 11, 2013 6:19 AM
    Moderator

All replies

  • Take a look at the example at the end of the documentation for Geolocator.GetGeopositionAsync.
    Tuesday, September 10, 2013 12:47 AM
  • Hello,

    I looked at it  but it doesn't handle the following exception for example.

    I create this exception by disabling networking just to simulate. here is the error. how do I handle this kind of exception I want to have my own error message. Thank you

    Tuesday, September 10, 2013 1:30 PM
  • Hi Avichai,

    Try catch block can have following structure, when you disconnect the Internet, the Exception will thrown and will be caught by the last Exception catch block, there you could handle all the exception that did not handled by System.UnauthorizedAccessException and TaskCanceledException.

    And what do you mean by have your own error message? I'm not quite understand this.

    Do you want to show the user a self-defined error message? The sample code use the TextBlock/TextBox to showing the message like "NO Data".  Or you want the system throw your error message.

    try
    {
        ...
    }
    catch (ExceptionTypeA e)
    {
        ...
    }
    catch (ExceptionTypeB e)
    {
        ...
    }
    catch (Exception e)
    {
        //for the exceptions that did not caught above.
        ...
    }

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, September 11, 2013 6:19 AM
    Moderator