November 27, 2014
Hot Topics:

Putting an Object in a Safe State

  • January 26, 2005
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Checking for Problems and Attempting to Recover

Checking for potential problems, catching the mistake, and attempting to recover is a tremendously better solution than simply checking for problems and aborting. In this case, the problem is detected by the code, and the application attempts to fix itself. This works well in certain situations. For example, consider the following code:

if (a == 0)   a=1;c = b/a;

It is obvious that if the if statement is not included in the code, and a zero makes its way to the divide statement, you will get a system exception because you cannot divide by zero. By catching the exception and setting a to 1, at least the system will not crash. However, setting a to 1 may not be a proper solution. You may need to prompt the user for the proper input value.

Although this means of error checking is preferable to the previous solutions, it still has a few potentially limiting problems. It is not always easy to determine where a problem first appears. And it might take a while for the problem to be detected. In any event, it is beyond the scope of this article to explain error handling in great detail. However, it is important to design error handling into the class right from the start.

Throwing an Exception

Most O-O languages provide a feature called exceptions. In the most basic sense, exceptions are errors that occur within a system. Exceptions provide a way to detect problems and then handle them. In Java and C++, exceptions are handled by the keywords catch and throw. This may sound like a baseball game, but the key here is that a specific block of code is written to handle a specific exception. This solves the problem of trying to figure out where the problem started and unwinding the code to the proper point.

Here is how the code for a try/catch block looks:

try {   // possible nasty code} catch(Exception e) {   // code to handle the exception}

If an exception is thrown within the try block, the catch block will handle it. When an exception is thrown while the block is executing, the following occurs:

  • The execution of the block is terminated.
  • The catch clauses are checked to determine whether an appropriate catch block for the offending exception was included (there may be more than one catch clause).
  • If none of the catch clauses handles the offending exception, then it is passed to the next higher-level try block (if the exception is not caught in the code, then the system ultimately catches it and the results are unpredictable).
  • If a catch clause is matched (the first match encountered), the statements in the catch clause are executed.
  • Then execution resumes with the statement following the try block.

Again, it is beyond the scope of this article to explain exception handling in great detail. Suffice it to say that exceptions are an important advantage for O-O programming languages. Here is an example of how an exception is caught in Java:

try {   // possible nasty code   count = 0;   count = 5/count;} catch(ArithmeticException e) {   // code to handle the exception   System.out.println(e.getMessage());   count = 1;}System.out.println("The exception is handled.");

Exception Granularity

You can catch exceptions at various levels of granularity. You can catch all exceptions or just check for specific exceptions, such as arithmetic exceptions. If your code does not catch an exception, the Java runtime willand it won't be happy about it!

In this example, the division by zero (because count is equal to 0) within the try block will cause an arithmetic exception. If the exception was generated (thrown) outside a try block, then the program would most likely have been terminated. However, because the exception was thrown within a try block, the catch block is checked to see if the specific exception (in this case, an arithmetic exception) was planned for. Because the catch block contains a check for the arithmetic exception, the code within the catch block is executed, thus setting count to 1. After the catch block executes, the try/catch block is exited and the message, The exception is handled. appears on the Java console (see Figure 5).



Click here for a larger image.

Figure 5: Catching an exception.

If you had not put ArithmeticException in the catch block, then the program would likely have crashed. You can catch all exceptions by using the following code:

try {   // possible nasty code} catch(Exception e) {   // code to handle the exception}

The Exception parameter in the catch block is used to catch any exception that might be generated within a try block.

Bulletproof Code

It's a good idea to use a combination of the methods described here to make your program as bulletproof as possible to your user.





Page 3 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel