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?
SOAP faults are generated by receivers to report business logic errors or unexpected conditions. In JAX-WS, Java exceptions ( java. lang. Exception ) that are thrown by your Java Web service are mapped to a SOAP fault and returned to the client to communicate the reason for failure.
One way is writing your custom interceptor which implements Spring WS's ClientInterceptor interface. You should override handleFault method to handle SOAP faults with your custom logic. Then you need to register your custom Interceptor class as an interceptor at your SOAP client config class.
A SOAP fault is an error in a SOAP (Simple Object Access Protocol) communication resulting from incorrect message format, header-processing problems, or incompatibility between applications.
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.
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With