Friday, 24 May 2013

Java Interview Questions - Exception Handling


What are Checked and UnChecked Exception?
Checked: subclass of Exception except RuntimeException. Unchecked: Error, RuntimeException and their subclasses. For unchecked exceptions, the compiler does not force the programmer to either catch the exception or declare it in the throws clause.

What is the difference between and error and an exception?
Errors should be irrecoverable. However there’s nothing stopping you from catching any subclass of Throwable.

How to create custom exceptions?
Extend Throwable or any of its subclasses, like Error, RuntimeException or Exception.

If I want an object of my class to be thrown as an exception object, what should I do?
Either make your class extend Throwable or any of its subclasses or delegate throwing your object to a wrapper subclass of Throwable.

If my class already extends from some other class what should I do if I want an instance of my class to be thrown as an exception object?
Java doesn’t allow extending multiple classes (unlike C++). You should delegate throwing it to a wrapper class that extends Throwable.

How does an exception permeate through the code?
An unhandled exceptions moves up the call stack until it finds a matching catch block that will be executed. If the exception is not caught anywhere, the program exist with that error.

What are the different ways to handle exceptions?
try-catch blocks or declaration in throws clause.

What is the basic difference between the 2 approaches to exception handling: 1) try catch finally block and 2) specifying the candidate exceptions in the throws clause? When should you use which approach?
1) The method is responsible of dealing with it’s own exceptions. 2) Otherwise. Making the decision is subject to patterns and anti-patterns in exception handling, not a short and decisive answer.

Is it necessary that each try block must be followed by a catch block?
No. However, in this case, it must be followed by a finally block.

If I write return at the end of the try block, will the finally block still execute?
Yes. It can also change the return value.

If I write System.exit (0); at the end of the try block, will the finally block still execute?
How does a try statement determine which catch clause should be used to handle an exception?
The first matching catch block, in order of declaration.

Does it matter in what order catch statements for FileNotFoundException and IOException are written?
Yes, because the FileNotFoundException is a subclass of IOException.

What is the purpose of the finally clause of a try-catch-finally statement?
Execute code no matter if an exception is thrown or caught. Order of execution: try-catch-finally or try-finally-throw forward.

What are Chained Exceptions?

The chained exception feature allows you to associate another exception with an exception. This second exception describes the cause of the first exception. Lets take a simple example. You are trying to read a number from the disk and using it to divide a number. Think the method throws an Arithmetic Exception because of an attempt to divide by zero (number we got). However, the problem was that an I/O error occurred, which caused the divisor to be set improperly (set to zero). Although the method must certainly throw an Arithmetic Exception, since that is the error that occurred, you might also want to let the calling code know that the underlying cause was an I/O error. This is the place where chained exceptions come in to picture.
 Throwable getCause( ) Throwable initCause(Throwable causeExc)