Java Reference
In-Depth Information
In this case the throws keyword is used to advertise the exceptions
raised by the method. The general rule is that a method must declare all
checked exceptions that it throws, but not the unchecked exceptions.
This means that all Java exceptions that are part of the Error and
RuntimeException classes need not be advertised.
Exceptions Programming
The rationale behind the exception handling mechanism of Java is based on
two assumptions:
1. The compiler should ensure that all error conditions are adequately han-
dled by code.
2. It is better to handle errors separately from the program's main task.
The exception-handling strategy of a Java application should consider
the following issues:
1. Possible additional response to system errors. Code should not intercept
the system handler but may provide some additional diagnostics. For ex-
ample, upon detecting a system-level terminal error caused by a memory
shortage, an application may post a message warning the user of possible
loss of data.
2. Designing or modifying application code in order to avoid exceptions gen-
erated in the classes IOException and RuntimeException. For example,
making sure that a division-by-zero error is not produced by previously ex-
amining the divisor operand.
3. Handling exceptions generated by non-terminal conditions in Java built-in
classes. For example, providing code to recover from a file-not-found er-
ror.
4. Providing application-defined exception handlers for local error condi-
tions. For example, a user-developed method that finds the average value
in an integer array generates an exception if the user passes a null array as
the argument.
5. Deciding what part of the code should handle an exception. This means de-
termining if a particular exception should be handled within the method
that detects it, or if it should be propagated down the program's method hi-
erarchy to be addressed by another handler.
The first one of these listed issues requires no additional discussion.
The remaining four are examined later in this section.
Search WWH ::




Custom Search