Java exceptions interview questions, answers

This page contains the most common and practical Java exception handling interview questions with answers. Most of core java interviews have at least one question from exception handling.
Overview : Most Imp Questions Java Basic Interview Questions
OOPS Concepts Interview Questions Exception Handling Questions
MultiThreading & Concurrency API Java API : String, File I/O
Collection Framework Questions Advanced Questions

The exception is an event that occurs during execution of a program/application and disrupts the normal flow of the program. All exception in Java are subclass of 'Exception' class directly or indirectly. An exception can be handled by program and if done so it allows the program to continue execution.
Exception class hierarchy starts with 'Throwable' class which extends Object. Below diagram explains it:
exception
In Java an error is like an exception, only difference being it can't be handled and hence when it occurs the program gets terminated.
Checked Exception Unchecked Exception
They are subclass of Exception. All unchecked exceptions are subclass of RuntimeException.
Compiler checks if they are handled or not. Compiler don't check if they are handled in code.
Caused because of external things like IOException. Caused mostly because of programmer error like NullPointerException.
Yes. But you shouldn't. If you feel your code can throw some RuntimeException modify the code so it don't occur.
Checked : SQLException, IOException (FileNotFound), ClassNotFoundException, InterruptedException.
Unchecked : ArithmethicException, NullPointerException, ClassCastException, NumberFormatException, IndexOutOfBoundsException.
Throw Throws
Throw an exception. Used to declare that a method throws exception.
finally block gets executed always, irrespective of exception occured or not.
Yes, we can have finally block without catch block.
It is mandatory to have either catch or finally block for a matching try block.
Yes, finally block gets executed always.
When control will come to the last statement of try block, as soon as jvm see that it is a return statement, the statement don't get executed and the control flows to the finally block. It executes the finally block and then control comes back to the last statement of try block and the return statement gets executed.
Note: If last statement of finally block is also a return statement, the method returns from there itself and the return in try/catch block don't get executed.
No, this is the only scenario when finally block won't get executed.
System.exit(0) is the statement that tells jvm to terminate. So as soon as jvm sees this statement it simply exits.
Make a class that extends RuntimeException or Exception based on whether you want to make a checked or unchecked exception respectively.

public class TryCatchFinallyTrick { public static void main(String[] args) { System.out.println("The output is: " + getName()); } static int getName(){ int a = 3; try{ System.out.println("I am try"); a=4; return a; } catch(Exception e){ System.out.println("I am catch"); a=5; return a; } finally { System.out.println("I am finally."); a = 6; } } }

Output: I am try I am finally. The output is: 4

public class TryCatchFinallyTrick { public static void main(String[] args) { System.out.println("The output is: " + getName()); } static String getName(){ String name = "a"; try{ System.out.println("I am try"); name = "try"; return name; } catch(Exception e){ System.out.println("I am catch"); name = "catch"; return name; } finally { System.out.println("I am finally."); name = "finally"; return name; } } }

Output: I am try I am finally. The output is: finally

Make a class that extends RuntimeException or Exception based on whether you want to make a checked or unchecked exception respectively.
Make a class that extends RuntimeException or Exception based on whether you want to make a checked or unchecked exception respectively.



<< Previous Page : 1 2 3 4 5 6 7 8 Next >>