Try-Catch:
- Java API provides 5 keywords to handle exceptions.
- Keywords are try, catch, finally, throws and throw.
try:
- ‘try’ is a keyword.
- It is used to define a block.
- It contains doubtful code that raises exception.
- In case of runtime error, try block create exception object implicitly.
catch:
- It is a block.
- It is used to collect exception object that is created in the try block to be handled.
- Catch block executes only in case of exception in the try block.
try
{
1. Doubtful code.
2. Exception related code.
}
catch(Throwable obj)
{
Handling logic....
}
Default Exception Handler:
- It is a pre-defined program
- It is recommended to handle every exception by the programmer.
- If the programmer is not handled then “Default Exception Handler” handles the object with pre-defined error information.