- Nested try catch is possible
- Finally block is always executed even if exception is not handled. If try blocks give exception which is not handled then finally will be executed and exception will be thrown.
- Finally block in java can be used to put "cleanup" code such as closing a file, closing connection etc.
- If there is return statement in try block , then also finally will be executed
- If there is return statement in try and finally both the blocks then finally will override the return statement of try block. Only return statement of finally will be considered.
- With finally block we can skip catch block. Only try and finally will work.
- When Finally is not executed - If the JVM exits while the try or catch code is being executed, then the finally block may not execute. If the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues. The try block runs to the end, and no exception is thrown.
- All checked and unchecked exceptions can be handled by try-catch block. But we generally handle checked exception because most of unchecked exception should be handled by programmer itself like NullpointerException
- we can write multiple try blocks to handle exceptions or we can write multiple exceptions in single catch :
catch(SQLException | IOException e)
No comments:
Post a Comment