Declaring Exceptions in Database Control Methods

The database operations that are associated with methods in a Database control may result in error conditions. You have a choice whether to declare that your Database control method throws exceptions.

You may optionally declare that a method in a Database control throws the java.sql.SQLException, as shown here:

/**
 * @jws:sql statement="DROP TABLE customer"
 */
public void dropCustomerTable() throws SQLException; 

However, if you do so, you are forcing the user of your Database control to explicitly handle the exception by surrounding calls to this method in a try-catch block. For example, to call the dropCustomerTable method declared above on an control named custDB, your users will have to write code as follows:

try {
    custDB.dropCustomerTable()
}
catch (java.sql.SQLException sqle)
{
    // handle error here
}

If you do not declare that your methods throw exceptions, exceptions that are generated are still thrown. But they are wrapped as weblogic.jws.control.ControlException objects before being thrown. ControlException is a subclass of java.lang.RuntimeException. The advantage of RuntimeException is that it does not require that calls to methods that throw it to be wrapped in try-catch blocks. ControlException can still be caught by users of your Database control, but making it optional may increase convenience for users of your control.

If a ControlException (containing a SQLException) is thrown by your Database control method and is not handled in the calling JWS method, the calling JWS method will return the exception to its client. In most cases, this is not appropriate; the client code will most likely not be able to take action on the exception since the exception is related to the internal workings of the web service. It would be better design to handle the exception in the web service and return an appropriate error to the client if necessary.

To learn about handling exceptions in controls, see Handling Exceptions in Controls.

Related Topics

Database Control: Using a Database from Your Web Service

Creating a New Database Control