October 22, 2016
Hot Topics:

User Defined Exceptions: Improve Error Handling in Web Services

  • March 29, 2005
  • By Ayyappan Gandhirajan
  • Send Email »
  • More Articles »

When you generate stubs using the Weblogic clientgen utility, you would be able to catch MyCustomeException itself on the client side. The sample code is given below:

package WS.exception;
import java.text.MessageFormat;
public class Client {
   public static void main(String[] args ) throws Exception{

      ES_Impl ws = new ES_Impl(args[0]);
      ESPort port  = ws.getESPort();

      String returnVal = null;
         returnVal = port.echo(Integer.parseInt(args[1]),
                               "A for Apple");
   }catch(MyCustomException ex){
      System.out.println("MyCustomException occurred:");
      String errorCode = ex.getErrorCode();
      String errorDescription = ex.getErrorDescription();
      String[] variables = ex.getVariables();

      String resolvedErrorMsg = MessageFormat.format(
                                errorDescription, variables);
         //Authentication error: do something
      }else if(errorCode.startsWith("MSG")){
         //SOAP Message error: do something
         //General error: do something
   }catch(Exception ex){
      System.out.println("One of the other Exceptions occurred:");

In the program above, after catching MyCustomException, you can get the error code, text, and variables. Based on the error, you can segregate the type of error that had happened. If you want, you can use the text description that comes with the excerption. Otherwise, you can fetch the text description which in other language from a database, or a disk file that is associated with the error code. You can store the error code and description as a name-value pair in a disk file or a database. This will really help you manage internationalization of the error message. Also, MessageFormat class will help you get the dynamic error message.

SOAP Response 1 (when a SOAPFaultException is thrown)

Following is the SOAP response that I got while the service had thrown SOAPFaultException.

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
      <env:Fault xmlns:fault="">
         <faultstring>Number that you have entered is 1</faultstring>
         <faultactor>NO ACTOR</faultactor>
         <detail>Choice 1 means SOAPFaultException</detail>

SOAP Response 2 (when MyCustomException is thrown)

Following is the SOAP response that I got while the service had thrown MyCustomException.

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
         <faultstring>Service specific exception:
            <MyCustomException xmlns:n1="java:WS.exception"
               <errorCode xsi:type="xsd:string">GENEX001</errorCode>
               <errorDescription xsi:type="xsd:string">
                  Number that you have entered is {0}
               <variables soapenc:arrayType="xsd:string[1]">
                  <string xsi:type="xsd:string">2</string>


This article has discussed various exceptions in Web Service and how a user-defined exception could be used for effective error handling in Web Services.

About the Author

Ayyappan Gandhirajan holds a Bachelor's degree in Electronics & Communication Engineering from MK University, India and is pursuing a Master's in Software Systems from BITS, Pilani. He has been working as an Associate System Analyst for Hewlett-Packard, Bangalore. He has more than six years of software experience involving Web services, WS Security, and J2EE technologies. He is currently involved in Web services orchestration and access controllers. He can be reached at ayyappan.gandhirajan@hp.com or G_Ayyapparaj@yahoo.com.

Page 3 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel