Exception Handling Flashcards
What is exception handling?
Exception Handling is used to handle RunTime errors that occur in the JVM.
What are the differences between a Checked and Unchecked exception?
Checked exceptions extend Throwable, but not RuntimeException or Error classes. UncheckedException extend RuntimeException.
Checked are checked at compile time, unchecked happen at runtime.
What is the finally block in java?
Finally block is an optional block that can come with a try/catch block. Finally block is always executed after the execution of a try block.
What is the use of finally block?
Finally block is always executed wether an exception occurs or not. It helps in doing the cleanup like Rollback Transaction, Close connection etc.
Do we always have to put a catch block after a try block?
Java does not enforce the rule to put a catch block after a try block. You can write a catch block or a finally block after a try block.
What scenarios will a finally block not be executed?
If the program exits by calling system.exit() or a fatal error causes the JVM to crash.
What is the difference between throw and throws?
We use throw to explicitly throw an exception. We use throws to declare an exception in method definition. A call to throw occurs within a method, throws is just used with method signature.