Search This Blog

Monday, October 25, 2010

Java Exceptions and Error Handling Interview Questions

Q: What do you mean by exception and error handling?

A: When a program encounters and un-expected situation from where it cannot continue the normal flow, it can break from the normal execution flow by throwing and exception or an error.

Q: What is the difference between an Error and an Exception?

A: An Exception is an unexpected scenario from which the program can recover but an error means that the program has encountered an unrecoverable problem.

Q: Can you give an example of an unrecoverable problem when error is thrown?

A: Yes, when a java program runs out of memory it is a problem from which the program cannot recover and OutOfMemoryError will be thrown.

Q: Does it mean that when error is thrown, we cannot catch it and continue execution?

A: We can catch errors, but it is not advised to do that because when error happens it means that some unrecoverable problem has happened. Even if we catch the error, we cannot guarantee the stability of the application and something else might fail.

Q: What are the different kinds of Exceptions?

A: Checked Exceptions and Unchecked Exceptions.

Q: What are runtime Exceptions?

A: Un Checked Exceptions are also known as Runtime Exceptions.

Q: What do you mean by Checked Exceptions?

A: Checked exceptions are those exceptions which have to be explicitly handled in the code.

Q: Can we create our own checked exceptions?

A: Yes, we just have to extend the Exception class or any of its sub classes (except RuntimeException)

Q: How can we create our own runtime exception?

A: We have to extend the RuntimeException class or any of its sub classes.

Q: How can we create our own Errors?

A: We have to extend the Error class.

Q: What are the different approaches of Exception handling?

A: We can use Try Catch block or we can declare the exception in the method definition's throws clause

Q: Explain the difference between the two approaches of exception handling

A: We use the try catch clause where we want to process the exception and perform some action like displaying an error message. When an exception is caught using the catch clause, it will not be propagated to the caller.

We declare the exception in the throws clause of the calling method when the exception has to be propagated to the caller and the exception is supposed to be handled by the caller.

Q: What is finally?

A: Finally is also a block like catch block to be used with the try block. Control will come to finally block irrespective of whether exception was thrown or not.

Q: What is the difference between catch and finally?

A: When a catch block is used, we have to mention what exception has to be caught in the catch clause; where as in finally we need not mention anything. Code in the catch block is executed only when the exception is thrown whereas the code in finally is executed irrespective of whether exception was thrown or not.

Q: Can we have a try block without a catch block?

A: yes, then we must have a finally block. When try block is used, we must use either a catch block or a finally block or both.

Q: What happens when we use only try and finally block without a catch block?

A: When catch block is not used, the exceptions that are thrown will not be caught and the exception will be propagated to the caller, but before the exception is propagated, the code in the finally block will be executed.

Q: When do you use a catch block and when do you use a finally block?

A: We use a catch block when we want to handle the exception scenario. We use a finally block alone when we want to do some cleanup but at the same time propagate the exception to the caller. We use a finally block together with a catch block when we want to do something irrespective of what exception is thrown.

Q: what will happen if we return from the try block, will the finally block get executed?

A: yes, after the return statement in the try block is executed, the statements in the finally block will be executed before the control goes to the calling method.

Q: What happens when we have a return statement in the try block as well as in the finally block.

A: when we have return statement both in try and finally blocks, then first the return statement of try block gets executed, the before the method returns, the statements in the finally block will get executed and since there is return statement in finally also, that will also get executed and the value that will be returned to the caller will be the value returned from the finally block.

Q: If I write System.exit (0); at the end of the try block, will the finally block still execute?

A: No.

Q: Can I have more than one catch block following a try block?

A: yes,

Q: what are the rules for having multiple catch blocks?

A:

<!--[if !supportLists]-->1) <!--[endif]-->Each exception can be caught only once.

<!--[if !supportLists]-->2) <!--[endif]-->The catch block for sub class exceptions should come before e the catch block for parent class exceptions for example, FileNotFoundException extends IOException, so the catch block for FileNotFoundException should come before the catch block for IOException.

Q: Suppose I have a class A that has a method X which throws FileNotFoundException. I have a class B that extends class A and the method X is overridden in B. In the ovverriden method X in class B can I throw IOException?

A: No, because FileNotFoundException is a subclass if IOException and in the overridden method X in class B we can throw only FileNotFoundException or any other exception that extends FileNotFoundException.

Q: Is the above rule logical?

A: yes, the above rule is logical because java supports runtime polymorphism and at runtime an object of type B can be assigned to a variable of type A because B extends A, and when the method X is called on the variable of type A, the compile only check that the calling method should either catch or declare the exception that method X in class A is throwing, and if the method X in class B throws IOException, and the caller is handling only FileNotFoundException, then the exception will escape unhandled.

This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message.

 

2 comments:

Anonymous said...

informative blog.
good job. you rock...

srini

Anil Nivargi said...

Thanks...i visited another one blog also explained interview questions very nice please go through this blog http://adnjavainterview.blogspot.in/2014/06/exception-handling-questions.html