Exception Handling Models

820 Words2 Pages

Exception Handling Models
Two basic models available in Java
Termination
Resumption
Termination model: When an unrecoverable error occur while executing a program, then that program needs to be terminated without re-trying. These are also called unchecked exceptions (RuntimeException, Error, and their subclasses)
Ex: Stack overflow is an example for this. During an application execution, if Stack overflow error occurs, then application terminates and there is no way to recover from it
Resumption model: When a recoverable error occur during a program execution and it tries to recover from it. These are called checked exceptions(all classes except RuntimeException, Error and their subclasses). Well written program should anticipate errors and try for recovery
Ex: When trying to connect to internet error might occur (connection error), then you may want to try again
Exception Hierarchy
Any exception in java is an instance of a class derived directly or indirectly from the Throwable class
Throwable is the super class

Error and Exception classes are derived from Throwable class
There are numerous pre-defined errors that are derived from the Error and
RuntimeException classes
Exceptions derived from the Error object represent problems with the JVM and normally can’t be recovered and a little that a programmer will do with these. Stack overflow is an example of such an error
Exception types and it’s Hierarchy
Exception class support two types of exceptions:
Checked: These are exceptions that need to be dealt within the code
Checked exceptions include all exceptions derived from the Exception class and are not derived from the RuntimeException class
These must be handled in the code or the code will not compile cleanly, resu...

... middle of paper ...

...ys:
Specific error checking: For ex. You can check for a specific error like FileNotFoundException
Group error checking: For ex. You can check for a Group of errors like IOException. This IOException, is a generalized exception. Using this we can catch all I/O related errors
Any type of error: For ex. You can catch any type of errors by specifying Exception. This will catch any type error that occur during program execution. Exception is the super class and subclass of Throwable class
The Exception class is close to the top of the Throwable class hierarchy
You can define exception handlers to be as specific as possible
A handler must determine what type of exception occurred using recovery strategy
Exception handlers that are too general can make code more error-prone
Finally,
Grouping exceptions in a general fashion
Differentiating exceptions in an exact fashion

Open Document