|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Apart from the simplest programs, programs usually fail. Causes range from coding mistakes to bad user input, to operating system flaws. Regardless of the failure's cause, as a software developer, you're charged with designing programs that can either recover (where the program continues to execute after fixing the failure) or properly shut down (where the program makes every effort to save the user's data before terminating). After all, why should users pay for faulty programs that fail to save data when those programs unexpectedly terminate?
This article discusses program failure in the exception context. After defining exception, I show how C, C++, and Java handle exceptions. Knowing how to handle exceptions in C and C++ gives you an appreciation for why exception handling works the way it does in Java and lets you compare/contrast different exception-handling techniques.
Read the whole series on exception handling:
Situations arise where programs fail. For example, a program tries to open a file that does not exist. Or a program tries to access a nonexistent array element while ordering a name array. In those examples, the program's proper execution flow diverges into an abnormal flow. In the first example, that divergence occurs when the program fails to open the file. In the second example, the divergence occurs when the program tries to access the nonexistent array element. Each divergence from a proper execution flow to an abnormal one is known as an exception.
| Note |
|---|
| Additional examples of commonly occurring exceptions include attempts to write to a printer that is off, read from an unopened file, divide an integer by integer value zero, and call a method using an object reference variable containing a null reference. The first two examples illustrate resource-related exceptions because they involve resources (printer and file). The latter two examples illustrate flawed code exceptions because they arise from improperly written code. |
When an exception occurs, the program must handle that exception. Failure to do so puts the program into an unstable state. Under older operating systems, like Microsoft DOS, a program might crash the operating system and reboot the computer. Under newer operating systems, like Linux and Windows XP, the operating system might forcefully terminate the program. Or, instead of terminating, the program might behave erratically. How should a program handle exceptions? The next three sections provide an overview of exception-handling features that C, C++, and Java offer to programs.
C and many other languages have no technique for detecting -- much less handling -- exceptions arising from flawed code. For those languages, you must carefully write code so such exceptions never occur. In contrast, a technique has always been available for detecting and handling resource-related exceptions -- error code testing.