Chapter 11

Additional Web Questions

1) Checking return values is adequate for error handling, but exceptions should always be used for robust error handling.

a)_____ true

b)_____ false

Answer: B. Both techniques are very valuable for error detection and handling, but neither is better than the other in all cases. In most circumstances, exceptions are used for detecting and handling the unexpected. If failure or error conditions can be reasonably expected, the use of return values may be a better solution.

2) Only integer values can be used for error handling via return values.

a)_____ true

b)_____ false

Answer: B. Though integer values can be used for return value error handling, you are by no means limited to integer values. Null objects, empty strings and boolean flags are all very often used for error handling via return values.

3) If a program is written properly, no errors can occur during the runtime of the program.

a)_____ true

b)_____ false

Answer: B. Many problems may occur which are completely out of the control of the programmer. While diligence is a tremendous step towards error-free programs, the programmer cannot entirely control the environment under which the program runs—insufficient memory and hardware errors (to name a few problems) can cause unavoidable errors.

4) Advanced Java programmers use exceptions instead of conditional statements for improved flow-of-control throughout their programs.

a)_____ true

b)_____ false

Answer: B. Absolutely not! Exceptions should only be used to handle conditions that are unexpected—if an operation is likely to fail, standard error handling techniques involving return values should never be replaced with exceptions.

5) Inclusion of a finally block is optional.

a)_____ true

b)_____ false

Answer: A. The finally construct is completely optional.

6) Inclusion of a catch block is optional.

a)_____ true

b)_____ false

Answer: B. If you try, you must catch.

7) Although you can create your own exceptions, seldom will you find it useful to do so.

a)_____ true

b)_____ false

Answer: B. Creating your own exceptions can often be quite useful. The exceptions provided within the Java API address runtime issues inside and outside of the programmer’s control, as well as exceptions which support the classes available as part of the API. However, as you create your own classes, it will become clear when custom exceptions are necessary to promote solid programming.

8) All exceptions that are thrown by a method must be advertised by that method.

a)_____ true

b)_____ false

Answer: B. Ah, you did not forget about subclasses of RuntimeException, did you? These exceptions do not need to be advertised because often there is little that can be done to rectify the situation that caused them to be thrown.

9) It is possible to write a complete Java program without incorporating any exception handling code.

a)_____ true

b)_____ false

Answer: A. If you do not call methods that advertise exceptions or create methods that advertise exceptions, you will not be required to incorporate exception handling code into your program. Many simple (and not so simple) programs do not make use of methods that advertise exceptions, so the programmer has no need to incorporate exception handling.

10) When creating your own exception subclass, you must provide a default, empty constructor.

a)_____ true

b)_____ false

Answer: B. No, although it is customary to do so (and good practice), you are not required to provide a default, empty constructor for your exception class.