Of course, rules are (Web space) always somewhat subjective, and

Of course, rules are always somewhat subjective, and what is a valid reason to one developer may not be to another. You should be aware of the drawbacks and avoid using try/catch blocks other than for actual error conditions. 10.2 System Versus Application Exceptions Exceptions can be further classified into either system exceptions or application exceptions. System exceptions are more serious in nature. These are typically low-level problems that aren’t related to the application logic and from which end users are not expected to recover. In many cases, system exceptions are unchecked, and your application isn’t supposed to catch them because they are either non-programming errors or are so severe that nothing can be done about them. Application exceptions are errors that occur because of a violation of a business rule or some other condition in the application logic. For example, you might throw an application exception when a user attempts to log in to the application but the account has been locked. This isn’t a catastrophic error, but it is a problem that needs to be reported and handled. Within Struts applications (and web applications in general), there are essentially two approaches you can take when an exception occurs. If the exception is an application exception from which the end user may be able to recover, you typically want to return control back to the input page and display a user-friendly statement of the problem and some action that can be taken to resolve it. Continuing with the locked account example, you could throw an AccountLockedExceptionback to the action class, which would forward control back to the login page and inform the user that the account is locked. If the thrown exception is a low-level exception such as a RemoteException, the only meaningful action the application can take is to display a system error page. There’s nothing the user can do to fix the problem. It may be a programming error or some type of network issue, but the point is that you don’t want to let the user see the stack trace of the exception. Instead, forward to a system error page that’s more user-friendly to look at and optionally informs the user to notify the system administrator. The exception should also be logged to aid the developer in determining the root cause of the problem. Later in this chapter, you’ll see examples of how to return control back to the input page and show a localized message to the user. You will also learn ways of dealing with system errors by forwarding control to a system error page, all of which will add value to the application and to the user experience. 10.3 Using Chained Exceptions It’s often suitable to catch a particular type of exception and rethrow a different one. This is sometimes necessary because a client might not know or care to handle the original exception. For example, say that a client invokes an action on a Struts application to upload an image file to a database. Let’s further assume the Action class calls an update method whose signature looks like the following: public void updateImageFile( String imagePath ) throwsUploadException; When the method is called with an image to upload and a problem occurs, an UploadExceptionwill be thrown. However, the underlying problem will be more specific for example, the filesystem is full or the database already has the image, depending on the destination of the image upload. The
Check Tomcat Web Hosting services for best quality webspace to host your web application.

Leave a Reply