none
Get web response from System.Exception (Status Code) RRS feed

  • Question

  • I have a method that fetches data from a web service and does some other functions offline . I have put this code within try/ catch statements . Following is an example of what I'm trying to do .

     Is there a way that I can explicitly identify whether the thrown exception is a WebException ?  (to get responce)     

    private SampleMethod()
    {
      try 
     {
    // get data from web service
            // do other offline stuff
              }
     catch  (System.Exception ex)
              {
                  if (// if exception is a web service exception,)
                    {
                      // do these
                    }

                 else (// if exception is not a web exception)
               {
                   //do these
               }
        }

    }



    Monday, June 1, 2015 6:17 AM

Answers

  • You could use GetType() and match against typeof(WebException) -- but why? You can have multiple catch blocks for the try. Just let the runtime figure it out:

        private void SampleMethod()
        {
            try
            {
                throw new System.Net.WebException();
            }
            catch (System.Net.WebException wex)
            {
                // Handle WebException
            }
            catch(System.Exception ex)
            {
                // Handle Exception
            }
        }
    It's also generally considered a bad idea to catch Exception -- http://blogs.msdn.com/b/dotnet/archive/2009/02/19/why-catch-exception-empty-catch-is-bad.aspx
    Tuesday, June 2, 2015 10:59 PM
  • Hi Kalpa,

    According to your description, your issue is related to exception, the common language arithmetic forum is a better place for this issue, we will move it there for you.

    On the other hand, to check whether the exception is specify exception, you could use IS keyword to check it or you could catch specify exception directly.

    # try-catch (C# Reference)

    https://msdn.microsoft.com/en-IN/library/0yd65esw.aspx  

    Regards                      

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 2, 2015 1:43 AM
  • Hello Kalpa,

    Here is a demo sample according to Starain:

    static void Main(string[] args)
            {
                try
                {
                    int z = 0;
                    int a = 1 / z;
    
                    Console.ReadLine();
                }
                catch (Exception ex)
                {
                    if (ex is DivideByZeroException)
                    {
    
                    }
                }
            }
    You could check it.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, June 2, 2015 2:21 AM
    Moderator

All replies

  • Hi Kalpa,

    According to your description, your issue is related to exception, the common language arithmetic forum is a better place for this issue, we will move it there for you.

    On the other hand, to check whether the exception is specify exception, you could use IS keyword to check it or you could catch specify exception directly.

    # try-catch (C# Reference)

    https://msdn.microsoft.com/en-IN/library/0yd65esw.aspx  

    Regards                      

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 2, 2015 1:43 AM
  • Hello Kalpa,

    Here is a demo sample according to Starain:

    static void Main(string[] args)
            {
                try
                {
                    int z = 0;
                    int a = 1 / z;
    
                    Console.ReadLine();
                }
                catch (Exception ex)
                {
                    if (ex is DivideByZeroException)
                    {
    
                    }
                }
            }
    You could check it.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, June 2, 2015 2:21 AM
    Moderator
  • You could use GetType() and match against typeof(WebException) -- but why? You can have multiple catch blocks for the try. Just let the runtime figure it out:

        private void SampleMethod()
        {
            try
            {
                throw new System.Net.WebException();
            }
            catch (System.Net.WebException wex)
            {
                // Handle WebException
            }
            catch(System.Exception ex)
            {
                // Handle Exception
            }
        }
    It's also generally considered a bad idea to catch Exception -- http://blogs.msdn.com/b/dotnet/archive/2009/02/19/why-catch-exception-empty-catch-is-bad.aspx
    Tuesday, June 2, 2015 10:59 PM
  • Thank you very much =) 
    Wednesday, June 3, 2015 10:56 AM