Java Reference
In-Depth Information
Listing 5.1
Validation is a good reason to separate execute and initialize
public void initialize()
throws
AddPostValidationException,
IOException,
DataException {
try {
if (getAuthor() == null) {
throw new AddPostValidationException(
"Author field is required.");
}
if (getSubject() == null) {
throw new AddPostValidationException(
"The Subject field is required.");
}
if (getPostText() == null) {
throw new AddPostValidationException(
"The post is empty.");
}
if (getBoard() == null) {
throw new AddPostValidationException(
"The board is not valid.");
}
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
o Validate author
o Validate subject
o Validate postText
o Validate board
String url = "jdbc:db2:board";
// connect with default id/password
connection = DriverManager.getConnection(url);
} catch (Throwable theException) {
theException.printStackTrace();
}
}
In this initialize, we introduce validation. Here, we raise our own exception,
called AddPostValidationException . If at all possible, we should keep from
throwing a bare exception. This new exception serves as a collection point for
information about the error condition. It also clearly communicates the prob-
lem with the exception name. The code annotations show the four points
where we check the required fields, and if they haven't been set, we throw a
specialized exception. In a more advanced implementation, our controller
would use the information in the exception and populate a bean that could be
used by our standard error JSP .
Search WWH ::




Custom Search