Two Laptop Bag
The moose likes Web Services and the fly likes Proper Exception handling in WebServices Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Proper Exception handling in WebServices" Watch "Proper Exception handling in WebServices" New topic

Proper Exception handling in WebServices

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


I have developed a web service in java based on JAX WS approach , now i need to implement Exception Handling in my service .

Please tell me whether i should use SOAPFaultException or UserDefinedException .

What will be the best approach ??
Please share your ideas .

Save India From Corruption - Anna Hazare.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Seems to me it is preferable to stick to the existing standard if you can get enough information on the reason into the SOAPFaultException and get it out on the client side.

Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
My personal choice would be to use service-specific exceptions, with an eye on the JAX-WS 2.1 specification document, sections 2.5, 3.7 and 6.4.
If you are doing Java-first development, JAX-WS will help you translate the service-specific exceptions to appropriate faults.
When developing a JAX-WS client and if the service declares custom faults, these will be translated into Java exceptions.
SOAPFaultException is the basic type of exception being able to carry SOAP related information and faults that are not mapped to any exception will cause this exception to be thrown on the client side.
Clients to services using service specific exceptions will also be able to use the regular Java exception-handling mechanism in dealing with errors, without having to implement error handling based on the contents of a SOAPFaultException.
Best wishes!
Hany Shafik
Ranch Hand

Joined: Jun 21, 2008
Posts: 80

My personal point of view is that exceptions resulting from unanticipated run-time error should be thrown as SOAPFaultException, other validation error or business exception should be mapped to soap fault as custom java exception.
The following is quoted from book "SOA using java Web Services" chapter 7

I distinguish
between Java exceptions that can reasonably be mapped to a wsdl:fault
message (mapped exceptions) and those that have to be represented as
generic SOAP faults (unmapped exceptions). As an example, it is likely that
when an exception is thrown because input message validation fails, this
would be a mapped exception because the WSDL writer could anticipate
such an exception and define a wsdl:fault message for it. On the other
hand, exceptions resulting from unanticipated run-time errors most likely
will not have a corresponding wsdl:fault message and will need to be
returned as generic SOAP faults.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thank you all .

Hany , you made it look simple .
I agree. Here's the link:
subject: Proper Exception handling in WebServices
It's not a secret anymore!