Exceptions can be categorized in two types:
Checked Exception
If it is must to check an exception then it is called a checked exception. The program will not compile if there is a possibility of occurring checked exception in a try block and it is not handled.
The compiler ensures that if a method can throw a checked exception, directly or indirectly, then the method must explicitly deal with it. The method must either catch the exception and take the appropriate action, or pass the exception on to its caller.
Note:
Exception and all of its sub-classes (Excluding RuntimeException and its sub-classes) are checked exceptions and must be caught.
Unchecked Exception
If it is not must to check an exception then it is called an unchecked exception. The program will compile even if the unchecked exception is not handled.
- Unchecked Exception
- Checked Exception
Checked Exception
If it is must to check an exception then it is called a checked exception. The program will not compile if there is a possibility of occurring checked exception in a try block and it is not handled.
The compiler ensures that if a method can throw a checked exception, directly or indirectly, then the method must explicitly deal with it. The method must either catch the exception and take the appropriate action, or pass the exception on to its caller.
Note:
Exception and all of its sub-classes (Excluding RuntimeException and its sub-classes) are checked exceptions and must be caught.
Unchecked Exception
If it is not must to check an exception then it is called an unchecked exception. The program will compile even if the unchecked exception is not handled.
If any unchecked exception occurs in a program which is not handled then the program execution will terminate at that point. But if exception is handled then program will not terminate because of exception.
Note:
Note:
- Error and all of its subclasses are unchecked exceptions and need not be caught.
- RunTimeException and all of its sub-classes are unchecked exceptions and need not be caught.
No comments:
Post a Comment