getRootCause( ).printStackTrace(writer); } writer.flush( ); } } The (Florida web design)
getRootCause( ).printStackTrace(writer); } writer.flush( ); } } The exception class in Example 10-1 allows you to wrap the original Throwable with an instance of this exception class or any of its descendants. The nice thing about this feature is that it allows you to abstract out the ugly details of lower-level exceptions, while at the same time keeping those details available so they can be printed out to a log and used by developers. Because these exceptions can be chained together endlessly, this concept is commonly referred to as exception chaining. Exception chaining is an excellent way of preventing lower-layer abstractions, such as JDBC access, from propagating outward to an end user. The end user doesn’t care about the lower-layer problem, and abstracting the problem to a higher-level exception will keep him from seeing many of the details. Another benefit of using a higher-layer exception class is that the API of the upper layer is not tied nor coupled to the details of the implementation. Continuing our earlier example, suppose that a filesystem is initially used to store the images, and therefore the throws clause of the updateImageFile( )method declares that it throws an IOException. If later the implementation changes to use JDBC instead, the throws clause and the clients invoking the method must be changed to declare or catch a SQLException. By using a higher level of abstraction, the client only needs to be concerned about the UploadException, regardless of the underlying implementation. 10.3.1 Dealing with Multiple Exceptions A slight variation on the exception chaining idea is the concept of throwing multiple exceptions from a method. For example, say a user is filling out a form that has several price fields that must fall between some minimum and maximum values. Let’s further assume the price values can be validated only on the backend and not within an ActionForm. Unless your application can throw multiple exceptions from a method, the user will see only one exception at a time. This approach will work, but will probably become very annoying for end users, who will have to fix one field and then resubmit only to receive the next error. It would be easier for users if all of the errors were displayed and could be fixed at the same time. Unfortunately, a Java method can throw only a single instance of Throwable. One solution to dealing with multiple exceptions is to allow an exception class to have a primary exception while supporting a collection of other exceptions. Each exception can be treated the same, but the primary exception is used when only a single exception occurs, and the client can check the exception collection to see if there are more. Example 10-2 illustrates what the BaseException class from Example 10-1 would look like with this feature added to it. Example 10-2. An exception class that supports multiple nested exceptions package com.oreilly.struts.framework.exceptions; import java.util.List; import java.util.ArrayList; import java.io.PrintStream;
In case you need quality webspace to host and run your web applications, try our personal web hosting services.