views:

263

answers:

3

I throw a few soap exceptions in my web service successfully. I would like to catch the exceptions and access the string and ClientFaultCode 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?

+1  A: 
catch (SoapException soapEx) 
{
  //Do something with soapEx
}
Chad
+2  A: 

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.
}
Ben S
`final` is in Java only.
Roman Boiko
Whoops. C# smells too much like Java sometimes. I fixed my code example.
Ben S
Just to be sure Lou won't break his code with this change, he may add an additional block `catch (Exception e)`. sometimes it is possible that the exception is not `SoapException`... e.g.: `OutOfMemoryException`, or connectivity problem, or thread abort, etc.
Roman Boiko
This works well. However, I have a question. How can I find out what the string is really called so I can access it? For example: e.GetString() or something. For this particular example, I want to access the string "You lose the game."
Louise
I can call e.Message which returns a very long error message, but I only want the string message.
Louise
Louise, I don't know your situation, but I guess you can create classes inherited from `SoapException` with whatever additional information you need. Then add catch blocks for these classes **before** catching as described here.
Roman Boiko
Louise: `e.Message` returns **exactly** the same string as was passed to constructor, I tested. Maybe you used some other exception class, that added extra information to your message?
Roman Boiko
Or you handled another exception, which wasn't thrown by you. Such exception may occur at service-side, and it would be **wrapped** to `SoapException` by CLR.
Roman Boiko
Also note, that `SoapException.ClientFaultCode` according to MSDN "Specifies a SOAP fault code that represents a client call that is not formatted correctly or does not contain the appropriate information." It it exactly what you wish? Maybe, you should use another code, or another constructor. Just warning.
Roman Boiko
Thank you. All of this has been very helpful.
Louise
+1  A: 

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
}
codemeit
this doesn't answer Louise's question
Roman Boiko
added details in x.Message
codemeit