You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-user@ws.apache.org by Rajasekhar <ra...@deploy.com> on 2001/10/22 21:08:33 UTC

Java Exceptions Vs. SOAP fault

Hi,

I am trying to understand how exception handling works  with SOAP.  My
service on the server side can throw an exception (either MyException1 or
MyException2) based on specific condition on the server side. When the
server throws an exception, all I am getting at the client side is a Falut
(with fault code SOAP-ENV:Server) and faultString.  I am missing the error
code with the exception thrown at the server. Here is the sample code, I am
trying:

public class MyException1 extends Exception {
   public static int REASON1 = 100;
   public static int REASON2 = 200;

   private int errCode;
   MyException1(int _errCode, String msg)
   {
      super(msg);
      errCode = _errCode;
   }
}

public class MyException2 extends Exception {
   public static int REASON1 = 555;
   public static int REASON2 = 666;

   private int errCode;
   MyException1(int _errCode, String msg)
   {
      super(msg);
      errCode = _errCode;
   }
}

public class MyServer {
   public void testIt(int testCondition) throws ServiceException1,
ServiceException2
   {
      if(testCondition == 1)
      {
         throw new MyException1(MyException1.REASON1, "this is exception1");
      }
      else if(testCondition == 2)
      {
         throw new MyException2(MyException2.REASON1, "this is exception2");
      }
   }
}

Client:


import java.net.URL;
import java.util.Vector;
import java.util.Iterator;
import org.apache.soap.SOAPException;
import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;

public class MyClient {

     public testIt() throws MyException1, MyException2
     {
           URL url = new url("http://blah..blah");
         Call call = new Call();
           Vector param = new Vector();
         params.addElement(new Parameter("testValue",Integer.class, new
Integer(10), null));
         call.setParams(params);
         call.setTargetObjectURI("urn:MyServer");
         call.setMethodName("testIt");
         call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

         // Invoke the call.
         Response resp = null;
         try
         {
            resp = call.invoke(url, "");
         }
         catch( SOAPException e )
         {
            System.err.println("Caught SOAPException (" + e.getFaultCode() +
"): " + e.getMessage());
            System.exit(-1);
         }

         // Check the response.
         if( !resp.generatedFault() )
         {
            Parameter ret = resp.getReturnValue();
         }
         else
         {
            Fault fault = resp.getFault();
            System.err.println("Generated fault: ");
            System.err.println(fault.toString());

            Vector entries = fault.getDetailEntries(  );
            for (Iterator i = entries.iterator(); i.hasNext(  ); ) {
                org.w3c.dom.Element entry = (org.w3c.dom.Element)i.next(  );
                System.out.println(entry.getFirstChild().getNodeValue(  ));
            }

            //Find out what is the exception thrown by the server and reason
for the exception
            //recreate the exception object (MyException1 or MyException2)
and throw it to the
            //above layers for more proessing.??????????? How do I do this

         }
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
   }
}

Thanks in advance for any suggestions and I appreciate, if somebody helps me
with some useful example code.

- Raj.