Advantages of Exception Handling – Exception Handling

7.8 Advantages of Exception Handling

Robustness refers to the ability of a software system to respond to errors during execution. A system should respond to unexpected situations at runtime in a responsible way. Applications that provide the user with frequent cryptic messages with error codes or that repeatedly give the user the silent treatment when something goes wrong can hardly be considered robust.

The exception handling mechanism in Java offers the following advantages that facilitate developing robust applications in Java:

  • Separation of exception handling code

The code for handling error situations can be separated from the code for the program logic by using the exception handling constructs provided by the language. Code that can result in error situations is confined in the try block, and their handling in the catch clause.

  • Transparent exception propagation

Propagation of a checked exception in the JVM stack cannot be ignored by an active method. The method must comply with the catch-or-declare requirement: Either catch and handle the exception, or propagate it by declaring it in the method’s throws clause. Error situations causing exception propagation are thus always detected, and can be caught and remedied.

  • Exception categorization and specialization

The exception and error classes in the Java SE Platform API are organized in an inheritance hierarchy (Figure 7.3, p. 369). Classes higher in this hierarchy represent categories of exceptions and errors (Exception, RuntimeException, IOException, Error), whereas classes lower in this hierarchy represent more specific exceptions and errors (NullPointerException, FileNotFoundException, AssertionError). The try-catch construct allows flexibility in catching and handling exceptions. A catch clause can specify an exception category for coarse-grained exception handling, as the exception category class will subsume its more specific exception subclasses, or it can specify a more specific exception class for fine-grained exception handling. Best practice dictates that fine-grained exception handling be used.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *