Exception Handling Interview Questions

1 - What is an Exception?
An exception is any unusual condition that causes an alteration in program flow, such as a missing file or a null pointer. There are two types of exception
  • Checked Exception - exception that occur at compile time.
  • UnChecked Exception - exception that occur at runtime.

2 - What is Difference between Error and Exception?
Both Error and Exception extends Throwable.
  • Exception - They can be checked or unchecked, cause due to programmer or user fault and must handle at developer level.
  • Error - They are always unchecked, usually occur during system error. Like OutofMemoryError, StackOverflowError.

3 - Exceptions are defined in which java package?
Exceptions are defined in java.lang.Exception package.
4 - Explain the exception hierarchy in java

Exception Hierarchy

5 - What is Runtime Exception or unchecked exception?
Runtime or Unchecked exception are those exception which occur at runtime. Unchecked Exception are internal to the application and extends the java.lang.RuntimeException that is inherited from java.lang.Exception. These usually caused by data errors like arithmethic overflow, divide by zero, array out of bound etc.
6 - What is checked exception?
These are the exception which occur during the compile time of the program, the compiler checks at the compile time weather the program contains handlers for checked exceptions or not. These exception extends java.lang.Exception class. IOException, FileNotException etc are checked exceptiona.
7 - What is difference between ClassNotFoundException and NoClassDefFoundError?
  • ClassNotFoundException occurs when loader could not find the required class in class path.
  • NoClassDefFoundError occur when class is loaded in classpath, but one or more of the class which are required by other class, are removed or failed to load by compiler.

8 - What is throw keyword?
Throw keyword is used to throw the exception manually. The throw keyword is used to indicate that an exception occurred.
Syntax - throw throwableinstance //this is an instance of throwable
Example
try
{
    if(i <=10)
    {
        throw new IOException();
    }
}

9 - What is use of throws keyword?
Any exception that comes along throws keyword is a checked exception. When the method is used, if the exception is not handled, the program does not compile. Throws keyword throws exception to next class until main class handles it by surrounding it by try and catch block.
10 - What are the possible combination to write try, catch and finally block?
			
  • Try { //your code } Catch(Exception ee) { //handle exception } Finally { //The clean code which execute no matter exception occur or not }
  • Try { //your code } Finally { //The clean code which execute no matter exception occur or not }
  • Try { //your code } Catch(Exception ee) { //handle exception }

11 - How to create custom Exception?
To create your own exception you need to extend Exception class in java.lang.Exception. you can create both checked and unchecked exception
  • Extend Exception class to make your exception checked exception.
  • Extend Runtime Exception to make your exception unchecked exception.
Example
public class LimitExceedException extends Exception 
{
	public String toString()
	{
		return "Exceed Limit Exception,";
	}
}

12 - What is StackOverflowError?
StackOverFlowError is an Error object thrown when it encounters that your program has ran out of the memory.
13 - Is it valid to have a try block without catch or finally?
No, try block should have one or both blocks(catch or finally) immediately followed to handle the occurred exception.
14 - Can you catch more than one exceptions in a single catch block?
Yes, you can catch multiple exception in a single catch block by using Exception as a argument by following multiple if conditions.
Synatx
try
{
    /* something */
}
catch( Exception e )
{
    if (e instanceof IOException)
    {
       // It's IOException.
    }
}

15 - Is an empty catch block legal?
Yes you can have empty catch block but it is a bad practice.