Monday 6 February 2017

Exception Handling Interview Questions

1) What is Exception Handling? Exceptions are events that occur during the execution of programs that disrupt the normal flow of instruct... thumbnail 1 summary
1) What is Exception Handling?

Exceptions are events that occur during the execution of programs that disrupt the normal flow of instructions.When an exception occurs program processing gets terminated and doesn’t continue further. In such cases we get a system generated error message.

In Java, an exception is an object that wraps an error event that occurred within a method and contains:

  • Information about the error including its type
  • The state of the program when the error occurred
  • Optionally, other custom information
  • Exception objects can be thrown and caught.
Exception can occur at runtime (known as runtime exceptions) as well as at compile-time (known Compile-time exceptions).

There can be several reasons for an exception. For example, following situations can cause an exception – Opening a non-existing file, Network connection problem, Operands being manipulated are out of prescribed ranges, class file missing which was supposed to be loaded and so on.

2) What is difference between error and exception ? 

Errors indicate serious problems and abnormal conditions that most applications should not try to handle. Error defines problems that are not expected to be caught under normal circumstances by our program. For example memory error, hardware error, JVM error etc.
Exceptions are conditions within the code. A developer can handle such conditions and take necessary corrective actions. Few examples –
DivideByZero exception , NullPointerException , ArithmeticException , ArrayIndexOutOfBoundsException

Advantages : 

  • Exception handling allows us to control the normal flow of the program by using exception handling in program.
  • It throws an exception whenever a calling method encounters an error providing that the calling method takes care of that error.
  • It also gives us the scope of organizing and differentiating between different error types using a separate block of codes. This is done with the help of try-catch blocks.
3) What are Types of exceptions?

There are two types of exceptions

Checked exceptions
All exceptions other than Runtime Exceptions are known as Checked exceptions as the compiler checks them during compilation to see whether the programmer has handled them or not. If these exceptions are not handled/declared in the program, it will give compilation error.
Examples of Checked Exceptions :-
  • ClassNotFoundException
  • IllegalAccessException
  • NoSuchFieldException
  • EOFException etc.
Unchecked Exceptions
Runtime Exceptions are also known as Unchecked Exceptions as the compiler do not check whether the programmer has handled them or not but it’s the duty of the programmer to handle these exceptions and provide a safe exit.
These exceptions need not be included in any method’s throws list because compiler does not check to see if a method handles or throws these exceptions.
Examples of Unchecked Exceptions:-
  • ArithmeticException
  • ArrayIndexOutOfBoundsException
  • NullPointerException
  • NegativeArraySizeException etc.
4) What is Exception hierarchy?


5) Is it possible to have try block without a catch block?

It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

6) What is Finally Block?

A finally statement must be associated with a try statement. It identifies a block of statements that needs to be executed regardless of whether or not an exception occurs within the try block.
After all other try-catch processing is complete, the code inside the finally block executes. It is not mandatory to include a finally block at all, but if you do, it will run regardless of whether an exception was thrown and handled by the try and catch parts of the block.
In normal execution the finally block is executed after try block. When any exception occurs first the catch block is executed and then finally block is executed.
An exception in the finally block, exactly behaves like any other exception.
The code present in the finally block executes even if the try or catch block contains control transfer statements like return, break or continue.
Note :
The circumstances that prevent execution of the code in a finally block are:
  • The death of a Thread
  • Using of the System. exit() method.
  • Due to an exception arising in the finally block.
7) What is difference between throw and throws?
Throw keyword
  • throw is used to explicitly throw an exception.
  • checked exceptions can not be propagated with throw only.
  • throw is followed by an instance.
  • throw is used within the method.
  • You cannot throw multiple exception
Throws keyword
  • throws is used to declare an exception.
  • checked exception can be propagated with throws.
  • throws is followed by class.
  • throws is used with the method signature.
  • You can declare multiple exception e.g. public void method()throws IOException,SQLException.
8) What is exception propagation ?

An exception is first thrown from the top of the stack and if it is not caught, it drops down the call stack to the previous method,If not caught there, the exception again drops down to the previous method, and so on until they are caught or until they reach the very bottom of the call stack.This is called exception propagation.
Note :
  • By default Unchecked Exceptions are forwarded in calling chain (propagated).
  • By default, Checked Exceptions are not forwarded in calling chain (propagated).
9) How to handle ExceptionHandling with MethodOverriding ?

An overriding method (the method of child class) can throw any unchecked exceptions, regardless of whether the overridden method (method of base class) throws exceptions or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw those checked exceptions, which have less scope than the exception(s) declared in the overridden method. Read More....

No comments

Post a Comment