Gadgets

Thursday, 18 February 2016

Exception handling interview questions

An unwanted, unexpected event that disturbs the normal flow of program is called Exception. Exception handling doesn’t mean repair an exception; it provides the alternative solution to continue the rest of the program normally.
Here I provided most common frequently asking questions on Exception Handling.
1) What are the Exception Handling keywords?

  • try
  • catch
  • finally
  • throw
  • throws
2) What is the root class of Exception hierarchy?
Throwable is root class for the Exception and Error.

3) What is the different between Exception and Error?
Exceptions are caused by the programmer and it can be recoverable.
Error is caused by the system resources and it is non recoverable.

4) What is the difference between checked exception and unchecked exception?
Checked exceptions are checked by the compiler for the smooth execution of the program at runtime.
Example:- SQLException, IOException
Unchecked exceptions are at runtime but not at the compiler time.
Example:- ArithmeticException, ArrayIndexOutOfBoundException, NullPointerException 

5) What the difference is between fully checked and partially checked?
A checked exception and its child classes are also checked then it is fully checked.
A checked exception and its child classes are not checked then it is partially checked.

6) Explain the default Exception Handling mechanism in java?
If an exception raised inside a method then it will create an exception object and handover that object to the JVM, and JVM will check whether the method is contain any handling code or not.

7) What is the purpose of try block?
try block is used to place the code that may occur any exception. try block should followed by either catch or finally block.
Syntax for try-catch block:
try
{
}
catch ()
{
}

8) What is the purpose of catch block?
catch block is used to handle the Exception. We can use multiple catch blocks with single try block.

9) Is order of multiple catch blocks important?
If we use multiple catch blocks with try block then the order of catch blocks is important, it consider order from child exception class to parent exception.

10) What is the purpose of finally block?
finally block is always executed whether exception handle or not. finally block is used to clean up the important code such as closing database connection.

11) What are the various methods to print Exception information?
There are few methods to print exception information to the console.

  • printStackTrace()
  • toString()
  • getMessage()

12) If an Exception raise inside catch block then what will happen?
If an exception is raised inside catch block then it is abnormal termination, if any finally block followed with catch then finally block will be executed.

13) Is it possible to take try-catch blocks inside try block?
Yes, it is possible to take try-catch blocks inside try-catch block.

14) Is it possible to take try-catch blocks inside catch block?
Yes, we can take try-catch block inside catch block.

15) In which situation finally block will not execute?
Whenever we use System.exit(0) before executing finally block ( i.e. in try block ) then JVM shutdown itself so finally block won’t execute.

16) Is it possible to write any statement between try catch finally block?
We can’t write any other statements between try-catch-finally blocks.

17) Is it possible to take two finally blocks for same try block?
it is not possible to take two finally blocks for the same try block.

18) What is the difference between final, finally and finalize()?
final is a modifier, applicable for classes, methods and variables.
finally is a block, it is associated with try-catch blocks and it is used to clean up the code.
finaliz() is a method, it is called by the Garbage collector to clean up the activities before destroying the object.

19) What is the purpose of throw?
throw keyword is used to handover our created exception object to the JVM.

20) Is it possible to throw any java object?
We can use throw keyword only for throwable types, we can’t use for normal java classes.

21) What is the purpose of throws?
throws keyword is used to delegate the responsibilities of exception handling to the either JVM or method.

22) What is the difference between throw and throws?
throw keyword is used to handover our created exception object to the JVM. We can’t throw multiple exceptions.
throws keyword is used to delegate the responsibilities to either JVM or next method for handling the exception. We can throws multiple exceptions.

23) Is it possible to use throws keyword for any java class?
We can use throws keyword only for methods and constructors but not for classes.

24) What is the customized Exception?
If we place the risky code inside try block and write the catch block to handle that corresponding risky code is customized Exception.

25) Explain the control flow of try-catch-finally block?

Valid try-catch-finally block

Invalid try-catch-finally block

No comments:

Post a Comment