7 THE METHOD PARSEINTDOES NOT THROW AN EXCEPTION IF THE STRING ISOF...

17.7

The method

parseInt

does not throw an exception if the string is

of zero length. Amend it so that it throws the same exception in this

situation.

What happens after an exception has been handled? In the above example, the

catch

block ends with a

return

statement, which exits from the current method,

actionPerformed

and returns control to its caller. This is the appropriate action in

this case – the program is able to recover and continue in a useful way. In general the

options are either to recover from the exception and continue or to allow the program

to gracefully degrade. The Java language mechanism supports various actions:

handle the exception. Control flow then either continues on down the program or

the method can be exited using a

return

statement.

ignore the exception. This is highly dangerous and always leads to tears, probably

after the software has been put into use.

throw another exception. This passes the buck to another exception handler further

up the call chain, which the designer considers to be a more appropriate place to

handle the exception.

SELF-TEST QUESTION