8 WHAT HAPPENS IF THE RETURNSTATEMENT IS OMITTED IN THE ABOVE EXAMP...

17.8

What happens if the

return

statement is omitted in the above example

of the exception handler?

In the above example, the application program itself detected the exception.

Sometimes, however, it is the operating system or the hardware that detects an excep-

tion. An example is an attempt to divide by zero, which would typically be detected by

the hardware. The hardware would alert the run-time system or operating system,

which in turn would enter any exception handler associated with this exception.

The mechanism described above is the exception handling facility provided in Java.

Similar mechanisms are provided in Ada and C++.

In old software systems the simplest solution to handling exceptions was to resort to

the use of a

goto

statement to transfer control out of the immediate locality and into

a piece of coding designed to handle the situation. The use of a

goto

was particularly

appealing when the unusual situation occurred deep within a set of method calls. The

throw

statement has been criticized as being a

goto

statement in disguise. The

response is that

throw

is indeed a “structured

goto

”, but that its use is restricted to

dealing with errors and therefore it cannot be used in an undisciplined way.

In summary, exception handlers allow software to cope with unusual, but anticipated,

events. The software can take appropriate remedial action and continue with its tasks.

Exception handlers therefore provide a mechanism for forward error recovery. In Java,

the mechanism consists of three ingredients: