September 17, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Handling Anomalies: Errors and Exceptions

  • August 6, 2007
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Checking for Problems and Terminating the Application Gracefully

If you decide to check for potential problems and exit the application when a problem is detected, the application can, at least, display a message indicating that you have a problem and what action is being taken.

Detecting a problem and then gracefully exiting means that the application identifies a problem, cleans up the mess as best as possible, and then decides to terminate the application. This is not a crash. Rather than allowing the operating system to indiscriminately abort the application, the application (via the programmer) remains in control of the process. Then, the programmer simply decides that it is better, and/or safer, to exit the application.

This strategy allows the programmer to code clean-up methods that can perform important functions such as saving or closing files, sending messages, and putting the system in a safe-state. Consider an elevator system. If there is a fire alarm anomaly, you would want all the elevators to either proceed to the first floor, or at least open the doors at the closest floor. If the application simply crashes, there could be people left stranded in the elevators during a fire. This is all well and good; however, you better let the user know what is happening.

If you exit the application without letting the user know, the code may in fact terminate gracefully (meaning no crash); however, the user is left staring at the computer screen, shaking his/her head and wondering what just happened.

Although gracefully exiting the application when an anomaly is detected is a far superior option to ignoring the problem, it is by no means optimal. However, this does allow the system to clean up things and put itself in a more stable state, such as closing files.

// Class ErrorHandling
public class ErrorHandling {

   public static void main(String[] args) {

      int a = 0;
      int b = 0;
      int c = 0;

      if (b != 0) {    // catch the divide by zero
         c = a / b;
      } else {
         System.out.println("Error: Divide by zero");
         System.exit(0);    //abort under control
      }

   }

}

Listing 2: Generating an Error—Ignoring the exception

With the if statement, in the case of the code in Listing 2, you check to make sure that a division by zero never happens. When a division by zero is detected, the code is skipped and an error is printed. Diagram 3 shows the program flow in this situation. Note that the operating system eventually gets control of the process back; however, the application exits while in control and gives control back to the operating system voluntarily.



Click here for a larger image.

Diagram 3: An Object with two References and two Objects (different content)

The output produced when the code in Listing 2 is executed is presented in Figure 2. As you can see, the user is not shown the exception messages coming directly from the operating system. The users view what the programmer decides to show them.



Click here for a larger image.

Figure 2: Generating an Error—Catching the problem and aborting

Checking for Problems and Attempting to Recover

Checking for potential problems, catching the mistake, and attempting to recover is a far superior solution than simply checking for problems and gracefully exiting. In this case, the problem is detected by the code, and the application attempts to fix itself. This works well in certain situations. You could prompt the user to re-enter until an appropriate value is determined. Listing 3 is one solution—but not necessarily the best.

import java.io.*;

// Class ErrorHandling
public class ErrorHandling {

   public static void main(String[] args) throws Exception {

      int a = 0;
      int b = 0;
      int c = 0;

      b = getInput();

      while (b == 0) {    // catch the divide by zero

         System.out.println("Error: Divide by zero. Please reenter.");
         b = getInput();

      }

      c = a / b;
      System.out.println("c = " + c);
   }

   public static int getInput() throws Exception {

      int x;

      BufferedReader stdin =
         new BufferedReader(new InputStreamReader(System.in), 1);

      System.out.print("Please Enter Number:");
      String s1 = stdin.readLine();

      x = Integer.parseInt(s1);    // string to double

      System.out.println("x = " + x);

      return (x);

   }

}

Listing 3: Generating an Error—Catching the problem and handling it.





Page 3 of 5



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel