# Exception Handling using WSDL Faults

This sample demonstrates how to specify a WSDL fault in order to allow your service to communicate exception pathways to your clients.

Running of this sample assumes that you are running this within the extracted release folder.

## Constructing the Service and Client

The first step is to generate the service skeleton and other interface classes from the WSDL.

Look at bank.wsdl. It defines the interface for our service. Of particular interest are the AccountNotExistFault and InsufficientFundFault types defined in the wsdl:types element.

Using a command prompt, got to the folder of this example and type ant generate.service or just ant. This will:

- Generate the source for all the service classes in build/service/src/example
- Generate services.xml and a more complete BankService.wsdl into the build/service/resources folder.
- Generate a build/service/build.xml
- Copies the pre-populated service skeleton class to the generated code
- Compile the Java classes for the service
- Create the service archive and copy it to Axis2 repository (repository/services/) as sample-faulthandling.aar.

Open up service/src/example/BankServiceSkeleton.java and you will see the following code fragment inside #withdraw method. When generate.service has been executed, Axis2 generates a skeleton class (build/service/src/example/BankServiceSkeleton.java) which has an empty method. We replace it, the genedrated skeleton with a pre-populated skeleton class (service/src/example/BankServiceSkeleton.java) which has the following code inside it.

     final String account = param0.getAccount();
        if (account.equals("13")) {
        final AccountNotExistFault fault = new AccountNotExistFault();
        fault.setAccount(account);
        AccountNotExistFaultMessageException messageException = new
        AccountNotExistFaultMessageException("Account does
        not exist!");
        messageException.setFaultMessage(fault);
        throw messageException;
        }

        final int amount = param0.getAmount();
        if (amount > 1000) {
        final InsufficientFundFault fault = new InsufficientFundFault();
        fault.setAccount(account);
        fault.setBalance(1000);
        fault.setRequestedFund(amount);
        InsufficientFundFaultMessageException messageException = new
        InsufficientFundFaultMessageException("Insufficient
        funds");
        messageException.setFaultMessage(fault);
        throw messageException;
        }

        final WithdrawResponse response = new WithdrawResponse();
        response.setBalance(1000 - amount);
        return response;

Note that the source generated for the client will include the 2 faults and the local Exceptions through which they will be transmitted. Also note that the Exceptions are generated within the BankStub class.

## Deploying the Service

The above step must have already copied your BankService.aar file in to repository/services/ folder. Then go to bin folder and run either of axis2server.bat or axis2server.sh, depending on your platform in order to startup Axis2 server.

With the default configuration, if you go to http://localhost:8080/axis2/ you should see BankService was deployed.

## Running the Client

Invoke the client/src/example/BankClient.java class. You may use the command scripts to do so. You need to supply 3 parameters to the command- url, account and amount.

- ant run.client -Durl=http://localhost:8080/axis2/services/BankService -Daccount=13 -Damt=400
  Throws AccountNotExistFaultMessageException. You will see "Account#13 does not exist"
- ant run.client -Durl=http://localhost:8080/axis2/services/BankService -Daccount=88 -Damt=1200
  Throws InsufficientFundsFaultMessageException. You will see "Account#88 has balance of 1000. It cannot support withdrawal of 1200"
- ant run.client -Durl=http://localhost:8080/axis2/services/BankService -Daccount=88 -Damt=400
  Succeeds with a balance of 600. You will see "Balance = 600"

When you call ant run.client with parameters, before running client/src/example/BankClient.java class, it does the following as well:

- Generate the stubs (for the client) from the WSDL
- Compile the client classes
- Create a Jar of the client classes and copy it to build/client/BankService-test-client.jar