Interview Questions on Java - Exception Handling
1. What is Exception?
Exception is an abnormal condition that can not handled by JVM. Whenever Exception occurs, at that time program will be terminated.
2. What is the order when we use try,catch and finally blocks
3. If Exception occurs out side try block will it handle by the catch block.
No. The Exceptions raised in try block only can be handled by catch blocks.
4. How many arguments can be taken by catch block.
5. How many number of catch blocks can we associate with try block?
We can associate any no.of catch blocks including zero(but in this case finally block should be there).
6. What is finally block?
finally block is executed after try and catch blocks.finally block can be used optional.
7. If try block has return type then will finally block execute?
8. What is the situation that finally block doesn't execute?
If we use System.exit(); in try block then finally block won't be execute.
9. What are the types of Exceptions?
1. Checked Exception: The Exceptions which are known by Compiler.
2. Unchecked Exception: The Exceptions which will raised at the time of Running.
10. What is the difference between Exception and Error?
Exceptions are those which can be handled at the run time but errors cannot be handled.
Errors are unchecked exceptions and Exceptions can be both checked and unchecked exceptions.
11. Explain about throw,throws and Throwble?
1. throw : It is used to raise an exception explicitly.
2. throws: instead of using try and catch block we can avoid handling of checked exceptions with throws keyword. it is used at method definition.
3. Throwble: It is super class of Exception and Error classes.
12. Explain about final, finally and finalize?
1. final : final is a keyword which is used for classes, methods and variables.
final classes can not inherit to canother classes.
final methods can not override.
final variables can not change.
2. finally : finally block is used in exception handling and it is is executed after try and catch blocks.finally block can be used optional.
3. finalize(): It is a Object class method which will execute before the Garbage Collector clean the Objects.
13. Tell me some chechecked Exceptions?
ClassNotFoundException,FileNotFoundException and CloneNotSupportedExceptions.
14. Tell me some unchecked exceptions?
ArithmeticExcetion, ArrayIndexOutOfBoundException and NullPointerExceptions.
15. If Parent class method throws some exception then what type of exception could be thrown at child class?
It should be same type or subclass to that exception.