Interview Questions

What is the difference between a fault and exception in Apache SOAP?

SOAP Interview Questions and Answers


(Continued from previous question...)

What is the difference between a fault and exception in Apache SOAP?

The difference lies in where the error occurs - on the client side (during the generation of the soap request or the unmarshalling the response) - or on the server side (when unmarshalling the request, processing the message or marshalling the response). The client side raises an exception whereas the server side sends a SOAP response to the client indicating an error occured on the server side (ie. you get a SOAP fault).

For example, sending a SOAP encoded request for a method that does not exist results in a SOAP fault whereas if the server sends a response with a field that does not exist in a client side class a SOAP exception will be raised.

If you think about it it makes a lot of sense, as the server is the only entity that can realise the existence of server side errors but cannot raise exceptions client side, so instead sends a SOAP encoded message reporting the error. You can of course code into your client side to raise an exception in the case where you receive a server side fault. Here is the code (where you have pre-defined SoapClientException to contain 2 strings - the fault code and the fault string description):

//setup the call
Call call = new Call();
call.setSOAPMappingRegistry(mySmr);
call.setTargetObjectURI(myServiceName);
call.setMethodName(myMethodName);
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
call.setParams(myParams);

//invoke the call
Response resp = call.invoke(new URL(myServletURLName), "");
if (resp.generatedFault())
throw new SoapClientException(
resp.getFault().getFaultCode(),
resp.getFault().getFaultString() );

(Continued on next question...)

Other Interview Questions