| 1) Unchecked Exception
The exceptions that are not checked at compile time are called unchecked exceptions, classes that extends RuntimeException comes under unchecked exceptions. Examples of some unchecked exceptions are listed below.
2) Checked Exceptions
Exceptions that are checked at compile-time are called checked exceptions, in Exception hierarchy all classes that extends Exception class except UncheckedException comes under checked exception category.
|
| A checked exception is an exception that must be either caught or declared in a method where it can be thrown. For example, the java.io.IOExceptionis a checked exception. To understand what is a checked exception, consider the following code: |
| Unchecked, uncaught or runtime exceptions are exceptions that are not required to be caught or declared, even if it is allowed to do so. So a method can throw a runtime exception, even if this method is not supposed to throw exceptions. For example, ConcurrentModificationException is an unchecked exception.
The unchecked exceptions can only be the RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes must be handled, otherwise the compiler gives an error.
Sometime it is desirable to catch all exception for logging purposes, then throw it back on. For example, in servlet programming when application server calls the server doPost(), we want to monitor that no exception even runtime exception happened during serving the request. The application has its own logging separate from the server logging. The runtime exceptions would just go through without detecting it by the application. The following code would check all exceptions, log them, and throw it back again.
|
'Programming > Java' 카테고리의 다른 글
| predefined annotation /java (0) | 2014.06.27 |
|---|---|
| JUnit tutorial (0) | 2014.06.27 |
| Class.forName (0) | 2014.05.09 |
| JDNI - Java Directory & Naming Interface (0) | 2014.05.09 |
| jdk 1.5 - annotation / @ (0) | 2014.05.08 |
