none
dancko dancko 0 Points 3 Posts Handling Exception thrown by a Web Method in XML WebServices RRS feed

  • Question

  • Hi at all

    i have a doubt about handling exception in .NET (c#) Web Services.

    My doubt is this: where do i handle (try/catch) the exception thrown by a Web Method in a WebService Class?

    Suppose that we have a "Division" Web Method  that throw DivideByZeroException exception as the following:

    public class WebService1 : System.Web.Services.WebService
    {
    
        [WebMethod]
        public int Dividi(int a, int b)
        {
           if (b == 0)
           {
              throw new DivideByZeroException("Non Puoi dividere per zero");
           }
           return a / b;
        }
    }

    what i ask to you is:

    1) Where is the best way/place for handle (try/catch) that exception in the above example code?

    2) Can i handle (try/chatch) that exception in the application client method? For example a method in application client that call a Web Service.

    thanks in advances.

    Wednesday, October 16, 2013 8:54 PM

Answers

  • Hi,

    >>1) Where is the best way/place for handle (try/catch) that exception in the above example code?

    Please try to refer to the following code:

     public class WebService1 : System.Web.Services.WebService
    {
    
        [WebMethod]
        public int Dividi(int a, int b)
        {
               try
                {
                    return(a / b);
                }
                catch (Exception ex)
                {
                    throw new DivideByZeroException("Non Puoi dividere per zero");
                }
          }
        

    >>2)Can i handle (try/chatch) that exception in the application client method? For example a method in application client that call a Web Service

    When an error is encountered that make it impossible to successfully complete a web service method invocation, you should notify the client by returning a SOAP fault. To lean more about creating and returning SOAP faults to web service clients, please try to refer to:
    #Generating SOAP Faults from a Web Service.
    http://docs.oracle.com/cd/E13226_01/workshop/docs81/doc/en/core/index.html .

    Best Regards,
    Amy Peng


    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.

    Monday, October 21, 2013 2:13 AM
    Moderator

All replies

  • anyone have any idea?
    Thursday, October 17, 2013 8:10 AM
  • Please, anyone have any idea?

    thanks.

    Friday, October 18, 2013 1:28 PM
  • Hi,

    >>1) Where is the best way/place for handle (try/catch) that exception in the above example code?

    Please try to refer to the following code:

     public class WebService1 : System.Web.Services.WebService
    {
    
        [WebMethod]
        public int Dividi(int a, int b)
        {
               try
                {
                    return(a / b);
                }
                catch (Exception ex)
                {
                    throw new DivideByZeroException("Non Puoi dividere per zero");
                }
          }
        

    >>2)Can i handle (try/chatch) that exception in the application client method? For example a method in application client that call a Web Service

    When an error is encountered that make it impossible to successfully complete a web service method invocation, you should notify the client by returning a SOAP fault. To lean more about creating and returning SOAP faults to web service clients, please try to refer to:
    #Generating SOAP Faults from a Web Service.
    http://docs.oracle.com/cd/E13226_01/workshop/docs81/doc/en/core/index.html .

    Best Regards,
    Amy Peng


    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.

    Monday, October 21, 2013 2:13 AM
    Moderator