This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Web Services and the fly likes Custom Exception handling in Web services Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Web Services
Bookmark "Custom Exception handling in Web services" Watch "Custom Exception handling in Web services" New topic

Custom Exception handling in Web services

Reena Roy

Joined: Aug 08, 2008
Posts: 1
Hi All,
I have developed a web service using Apache Aixs 2 using eclipse/Apache tomcat 5.5 server
Web service takes 4 parameters and inserts in the Database.
But am not able to catch the custom exception which i have defined.I have also implemented Serializable for the custom exception which i have created.I have pasted the code below.Kindly help.

Code snippet below
Web service
try {

String dataSourceName = "webservice";
String dbURL = "jdbc dbc:" + dataSourceName;
con = DriverManager.getConnection(dbURL, "","");
System.out.println("Got connection"+con);
String sqlinsert="INSERT INTO EMP(id,Name,Age,Address,PhoneNumer) VALUES ("+ id+ ",'"+name+ "',"+ age+",'"+ address+ "',"+phoneno+")";
System.out.println("Sql insertion"+ sqlinsert);
pre = con.prepareStatement(sqlinsert);
result = pre.executeUpdate();
if(result !=0)
message = result +"Records Inserted";
message ="No Records Inserted";

catch(SQLException sqle){
System.out.println("SQL Exception in Webservice DBInsert :");
throw new MyException("SQLEX001","SQL Exception Caught");
catch (Exception err) {
System.out.println("General Exception in Webservice DBInsert " + err);
message ="Exception while insertion";
throw new MyException("GENEX001","General Exception Caught");


I have created a Portlet client and have deployed the same in Web sphere portal server 6.0 and the code snippet below:---
Web service Portlet Client
try {

tmp = request.getParameter("empId");
Integer intobj = new Integer(tmp);
empid = intobj.intValue();
empName = request.getParameter("empName");
empAddress = request.getParameter("empAddress");
tmp = request.getParameter("empAge");
Integer intobj = new Integer(tmp);
empAge = intobj.intValue();
tmp = request.getParameter("empPhone");
Integer intobj = new Integer(tmp);
empPhone = intobj.intValue();

InsertEmpInfoInsertEmpInfoSOAP11Port_httpStub stub = new InsertEmpInfoInsertEmpInfoSOAP11Port_httpStub();
InsertData insertobj = new InsertData();

InsertDataResponse res = stub.insertData(insertobj);
System.out.println("output: "+res.get_return());
message = res.get_return();
response.setRenderParameter("output", message);

catch (InsertDataFaultException e) {
// TODO Auto-generated catch block
System.out.println("DBInsertWSClient:--->Exception in Web Service");
System.out.println("Error Code "+e.getFaultMessage().getMyException().getErrorCode());
System.out.println("Error Description "+e.getFaultMessage().getMyException().getErrorDescription());


My custom Exception Class as follows



public class MyException extends Exception implements Serializable{
private static final long serialVersionUID = -7384814069415378236L;

private String errorCode;
private String errorDescription;

public MyException(String errorCode,
String errorDescription){
this.errorCode = errorCode;
this.errorDescription = errorDescription;


public String getErrorCode() {
return errorCode;

public void setErrorCode(String errorCode) {
this.errorCode = errorCode;

public String getErrorDescription() {
return errorDescription;

public void setErrorDescription(String errorDescription) {
this.errorDescription = errorDescription;

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Custom Exception handling in Web services
It's not a secret anymore!