Exception Handling Interview Questions in Java
Interview Questions on Exception Handling in Java
What is Exception Handling ?
The process of converting system error messages into user friendly error message is known as Exception handling.
What is Exception ?
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's Instructions.
Which is super class for any Exception class ?
Object class is super class for any Exception class.
Can any statement is possible between try and catch block ?
Each and every try block must be immediately followed by catch block that is no intermediate statements are allowed between try and catch block.
Can any try block contain another try block ?
Yes, One try block can contains another try block that is nested or inner try block can be possible.
Can finally block be used without catch ?
Yes but should follow "try" block then.
When IOException is thrown ?
IOException is thrown in following conditions which is given below;
- Try to transfer more data but less data are present.
- Try to read data which is corrupted
- Try to write but file is read only.
When ArithmeticException is thrown ?
The ArithmeticException is thrown when integer is divided by zero or taking the remainder of a number by zero. It is never thrown in floating-point operations.
Difference between throw and throws ?
throw | throws | |
---|---|---|
1 | throw is a keyword used for hitting and generating the exception which are occurring as a part of method body | throws is a keyword which gives an indication to the specific method to place the common exception methods as a part of try and catch block for generating user friendly error messages |
2 | The place of using throw keyword is always as a part of method body. | The place of using throws is a keyword is always as a part of method heading |
3 | When we use throw keyword as a part of method body, it is mandatory to the java programmer to write throws keyword as a part of method heading | When we write throws keyword as a part of method heading, it is optional to the java programmer to write throw keyword as a part of method body. |
Difference between checked Exception and un-checked Exception ?
Checked Exception | Un-Checked Exception | |
---|---|---|
1 | checked Exception are checked at compile time | un-checked Exception are checked at run time |
2 | for checked Exception Extend Throwable class except RuntimeException. | for un-checked Exception extend RuntimeException. |
3 | e.g. IOException, SQLException, FileNotFoundException etc. | e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, NumberNotFoundException etc. |
Is it necessary that each try block to be followed by catch block ?
It should be followed by either catch or finally block.
Explain throw, throws , try and catch in Java ?
throw: It is used to re throw an exception.
throws: It is used to declare that the method throws the respective exceptions.
catch: It is used to catch the exception that has been thrown by the respective try block.
Difference between Error and Exception
Error | Exception | |
---|---|---|
1 | Can be handle. | Can not be handle. |
2 | Example: NoSuchMethodError OutOfMemoryError | Example: ClassNotFoundException NumberFormateException |