Using and Handling SystemException
For demonstration,
Listing 5 uses SystemException to rewrite the
getConnection() method in
Listing 2. The method does not throw checked exceptions, so it can be used without worrying about those exceptions and without a "try/catch/do nothing" block.
Listing 5: getConnection Method Updated to Use SystemException
public static Connection getConnection()
{
Connection connection = null;
try
{
// Load the JDBC driver for your database
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
// Create a connection to the database, usually those values are retrieved
from a properties file
String serverName = "127.0.0.1";
String portNumber = "1521";
String sid = "mydatabase";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "username";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
return connection;
}
catch (ClassNotFoundException e)
{
throw new SystemException( e );
}
catch (SQLException e)
{
throw new SystemException( e );
}
}
Since SystemException is a runtime exception, it will move up the exception stack until it gets handled. Depending on your application, you can pick a suitable point in your code to handle it. For example, if you are developing a Web application based on MVC, you can do a try/catch for SystemException in the controller. Since such exceptions require technical intervention, you can do some logging in the catch for this exception or send an email to the administrator.
An alternative for a Web application would be declaring an exception handler in the web.xml as shown in Listing 6.
Listing 6: Error Pages Are Commonly Used in Web Applications
<?xml version = '1.0' encoding = 'windows-1256'?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
.
.
.
<error-page>
<exception-type>SystemException</exception-type>
<location>sys_exc_error.jsp</location>
</error-page>
.
.
.
</web-app>
Using and Handling ApplicationException
Working with ApplicationException requires more coding: you need to translate the generated exception into a meaningful exception for others using the method you developed.
Going back to the user registration example, Listing 7 shows how to create an
ApplicationException when the database unique constraint is violated. The method checks against the error message generated by an Oracle database, but this can be modified for any database.
Listing 7: Using ApplicationException
public static void registerUser() throws ApplicationException
{
try
{
Connection connection= Class1.getConnection3();
String sql= "INSERT INTO USERS (USER_NAME,PASSWORD) VALUES (?, ?) ";
PreparedStatement stmt= connection.prepareStatement(sql);
stmt.setString ( 1 , "test" );
stmt.setString ( 2 , "123" );
stmt.executeUpdate();
}
catch( SQLException sqe )
{
if ( sqe.getMessage().indexOf("ORA-00001") != -1 )
{
// This SQLException is meangful to my application business.
throw new ApplicationException( ApplicationException.UNIQUE_KEY ) ;
}
else
{
// This SQLException is meangless to my application business, it indicates
// a programmer mistake or infrastructure problems
throw new SystemException( sqe );
}
}
}
As you can see, ApplicationException contains constants representing common database errors. You can customize the error codes according to your need.
Listing 7 also shows how a java.sql.SQLException can be treated as either an ApplicationException or a SystemException depending on which makes sense from an application perspective. Other methods using this method will have to handle ApplicationException and act according to the error code it has.
Concentrate on Meaningful Exceptions
By using the technique described in this article, you concentrate on handling only the exceptions that are meaningful to your application. The number of try/catch blocks is also reduced, and you definitely need fewer lines of code.
You also can customize ApplicationException and SystemException to meet your requirementsyou can even extend them, if necessary.