开发者

How do you catch a thrown soap exception from a web service?

I throw a few soap exceptions in my web service successfully. I would like to catch the exceptions and access the string and ClientFaultCod开发者_如何学Goe that are called with the exception. Here is an example of one of my exceptions in the web service:

throw new SoapException("You lose the game.", SoapException.ClientFaultCode);

In my client, I try to run the method from the web service that may throw an exception, and I catch it. The problem is that my catch blocks don't do anything. See this example:

try
{
     service.StartGame();
}
catch
{
     // missing code goes here
}

How can I access the string and ClientFaultCode that are called with the thrown exception?


You may want to catch the specific exceptions.

try
{
     service.StartGame();
}
catch(SoapHeaderException)
{
// soap fault in the header e.g. auth failed
}
catch(SoapException x)
{
// general soap fault  and details in x.Message
}
catch(WebException)
{
// e.g. internet is down
}
catch(Exception)
{
// handles everything else
}


Catch the SoapException instance. That way you can access its information:

try {
     service.StartGame();
} catch (SoapException e)  {
    // The variable 'e' can access the exception's information.
}


catch (SoapException soapEx) 
{
  //Do something with soapEx
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜