Exception Interview Question

  1. What is Exception in java? What is Checked and UnChecked Exception in java?
  2. What is difference between checked and unchecked exception in java ?
  3. Explain exception in case of method overriding.
  4. Try, Catch and finally block.
  5. Custom exception in java and its use.
  6. Try with multiple catch example.
  7. The return statement in exception handling.

 

Q.What is Exception in java? What is Checked and UnChecked Exception in java?

This is the common question which is asked in interview. People know about answer but sometimes they struggle. so here see simple definition.
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s executions. It is of two types –

  1. Checked Exception
  2. UnChecked Exception

Checked Exception – 

All Exceptions which are not subclass of RuntimeException is calle Checked Exception. We must need to handle Checked exception at compile time by using throws keywords or by using try catch block

Let’s see couple of Examples of Checked Exception –

  1. IOException
  2. FileNotFoundException
  3. ClassNotFoundException
  4. CloneNotSupportedException
  5. NoSuchMethodException
  6. InterruptedException
  7. NoSuchFieldException
  8. ParseException

Will the below code compile –

Answer is no this code will not compile because –
A. The constructor defined public FileInputStream(String name) throws FileNotFoundException which is checked exception.
B.  close() method throws IOException which is also a checked exception.
Now see the solutions –

First Solution – As we know checked exception must be handle at compile time either by using throws keyword or by using try catch block. So here firstly we are using throws keyword.

Second Solution –  Here we are going to use try catch block.

UnChecked Exception :-

All Exceptions which are subclass of RuntimeException is called unchecked exception.we no need to handle to handle RuntimeExcepton by using throws keyword or try catch block , but if we want we can. if we do some programming mistake then jvm will automatically throw runtime exception. It will always happen at runtime.

Example of UnChecked Exception –

  1. NullPointerException
  2. ClassCastException
  3. ArrayIndexOutOfBoundsException
  4. IllegalStateException
  5. NumberFormatException
  6. IllegalArgumentException
  7. IllegalMonitorStateException
  8. IndexOutOfBoundsException

Let’s see example which throws NullPointerException –

Here this is simple code. This code will not give any compile time error , it will throw NullPointerException when you will try to execute.

What is difference between checked and unchecked exception in java ?

Let’s see difference between checked and unchecked exception –

Checked ExceptionUnChecked Exception
1.All Exception which is not subclass of RuntimeException is Checked Exception. 2.it happens at runtime.
3.In case of Checked Exception you must need to handle at compile time either by using try catch or throws keyword.3.it is not mandatory to handle unchecked exception.
4.Example is :-FileNotFoundException.4.Example is :-NullPointerException

Explain exception in case of method overriding?

  1. when super class method throws some exception – 

Subclass overridden method can declares subclass exception or same exception(i.e declares in super class) or no exception.

Example when declares subclass exception :-

 

Example when overridden method declares same exception :-

Example when overridden method declares no exception :-

2. when super class method doesn’t throws any exception – 

Subclass overridden method can declares only RuntimeException or unchecked exception not Checked exception.

 

Top