21.08.2013 Views

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

17.6 Recovery blocks 249<br />

■ throw another exception. This passes the buck to another exception handler further<br />

up the call chain, which the designer considers to be a more appropriate place to<br />

handle the exception.<br />

SELF-TEST QUESTION<br />

17.8 What happens if the return statement is omitted in the above example<br />

of the exception handler?<br />

In the above example, the application program itself detected the exception.<br />

Sometimes, however, it is the operating system or the hardware that detects an exception.<br />

An example is an attempt to divide by zero, which would typically be detected by<br />

the hardware. The hardware would alert the run-time system or operating system,<br />

which in turn would enter any exception handler associated with this exception.<br />

The mechanism described above is the exception handling facility provided in Java.<br />

Similar mechanisms are provided in Ada and C++.<br />

In old software systems the simplest solution to handling exceptions was to resort to<br />

the use of a goto statement to transfer control out of the immediate locality and into<br />

a piece of coding designed to handle the situation. The use of a goto was particularly<br />

appealing when the unusual situation occurred deep within a set of method calls. The<br />

throw statement has been criticized as being a goto statement in disguise. The<br />

response is that throw is indeed a “structured goto”, but that its use is restricted to<br />

dealing with errors and there<strong>for</strong>e it cannot be used in an undisciplined way.<br />

In summary, exception handlers allow software to cope with unusual, but anticipated,<br />

events. The software can take appropriate remedial action and continue with its tasks.<br />

Exception handlers there<strong>for</strong>e provide a mechanism <strong>for</strong> <strong>for</strong>ward error recovery. In Java,<br />

the mechanism consists of three ingredients:<br />

1. a try block, in which the program attempts to behave normally<br />

2. the program throws an exception<br />

3. a catch block handles the exceptional situation.<br />

17.6 ● Recovery blocks<br />

Recovery blocks are a way of structuring backward error recovery to cope with unanticipated<br />

faults. In backward error recovery, periodic dumps of the state of the system are<br />

made at recovery points. When a fault is detected, the system is restored to its state at the<br />

most recent recovery point. (The assumption is that this is a correct state of the system.)<br />

The system now continues on from the recovery point, using some alternative course<br />

of action so as to avoid the original problem.<br />

An analogy: if you trip on a banana skin and spill your coffee, you can make a fresh<br />

cup (restore the state of the system) and carry on (carefully avoiding the banana skin).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!