-
Essay / Exception Handling Patterns - 820
Exception Handling PatternsTwo basic patterns available in the JavaTerminationResumptionTermination pattern: When an unrecoverable error occurs during the execution of a program, that program should be terminated without retrying . These are also called unchecked exceptions (RuntimeException, Error and their subclasses). Ex: Stack overflow is an example. While running an application, if a stack overflow error occurs, the application terminates and there is no way to recover. Recovery Pattern: When a recoverable error occurs while running a program and attempts to recover from it. These are called checked exceptions (all classes except RuntimeException, Error and their subclasses). A well-written program should anticipate errors and try to recoverEx: When an error connecting to the Internet may occur (connection error), you may want to try againException HierarchyAny exception in Java is an instance of a directly derived class or indirectly from Throwable classThrowable is the super classError and Exception classes are derived from Throwable class. There are many predefined errors that are derived from the Error and RuntimeException classes. Exceptions derived from the Error object represent problems with the JVM and cannot normally be recovered and a few programmers will. do with those. Stack overflow is an example of such types of errors and its HierarchyException class supports two types of exceptions: Checked: These are exceptions that need to be handled in the code. Checked exceptions include all exceptions derived from the Exception class and are not derived from the RuntimeException class. must be handled in the code otherwise the code will not compile cleanly, result...... middle of paper ......ys: Specific error checking: E.g. You can search for a specific error like FileNotFoundExceptionGroup error checking: e.g. You can look for a group of errors like IOException. This IOException is a generalized exception. Using this we can detect all I/O related errors. Any type of error: e.g. You can catch any type of errors by specifying Exception. This will detect any type errors that occur while running the program. Exception is the super class and subclass of Throwable class. The Exception class is near 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 a recovery strategy. Too general exception handlers can make code more error-prone Finally, Group exceptions generally Differentiate exceptions exactly