Declaring Exceptions in Database Control Methods

The database operations associated with methods in a Database control may result in error conditions. You can choose whether or not to declare that your Database control method throws exceptions. For example, you can declare that a method in a Database control throws the java.sql.SQLException, as shown here:

/**
 * @jc: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 choose not to declare that your methods throw exceptions, exceptions that are generated are still thrown, but they are wrapped as com.bea.control.ControlException objects beforehand. ControlException is a subclass of java.lang.RuntimeException, which 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 can 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 method that calls it, then the calling method returns 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 because the exception is related to the internal workings of the application. It would be better design to handle the exception in the application and return an appropriate error to the client if necessary. 

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

Related Topics

Database Control

Creating a New Database Control