Java Exception Interview Questions and Answers

What is an Exception in Java?

What are the Keywords used for Exception Handling in Java?

Explain Java Exception Hierarchy?

Important methods of Java Exception Class?

Explain Java 7 ARM Feature and multi-catch block?

What happens when an exception is thrown by the main method?

What is OutOfMemoryError in Java?

What are different reasons for "Exception in thread main"?

What is the difference between final, finally, and finalize in Java?


Final:

The Final is the modifier applicable for class, methods, and variables.

If a class is declared as the final then child class creation is not possible.

If a method is declared as the final then overriding that method is not possible.

If a variable is declared as the final then reassignment is not possible.

Finally:

It is the block always associated with try-catch to maintain clean-up code which should be executed always irrespective of whether the exception is raised or not raised and whether handled or not handled.

Finalize:

It is a method that should be called by garbage collector always just before destroying an object to perform cleanup activities.

Can we have an empty catch block?

What is the difference between Checked and Unchecked Exceptions in Java?

What is the difference between the throw and throws keyword in Java?

How to write custom exceptions in Java?

What is the use of the Finally block for Exception Handling in Java? 

It is never recommended to take clean-up code inside a try block because there is no guarantee for the execution of every statement inside a try.
It is never recommended to place clean-up code inside the catch block because if there is no exception then the catch block won't be executed.
We require someplace to maintain clean up code which should be executed always irrespective of whether exception raised or not raised and whether handled or not handled such type of place is nothing but finally block.
Hence the main objective of finally block is to maintain cleanup code.

Example:
Try { //risky code } catch(x e) { //handling code } finally { //cleanup code }

In what situation where the finally block won't be executed?

There is only one situation where the finally block won't be executed, whenever we are using System.exit(0) method.
Then JVM itself will be shut down, in this case finally block won't be executed.
i.e., System.exit(0); dominates finally block.

class Test { public static void main(String[] args) { try { System.out.println("inside the try"); System.exit(0); } catch(ArithmeticException e) { System.out.println("the catch block executed"); } finally { System.out.println("the finally block executed"); } } } Output: inside the try

Note :

System.exit(0);

insteadof zero, we can take any integer value zero means normal termination, non-zero means abnormal termination this status code is internally used by JVM, whether it is zero or non-zero there is no change in the result and the effect is the same.

What are Best Practices for Java Exception Handling?

Whenever we are writing try block compulsory we should write either catch or finally. i.e., try without a catch or finally is invalid.

Whenever we are writing catch block compulsory we should write a try. i.e., catch without try is invalid.

class Test1 { public static void main(String[] args) { try { System.out.println("Compile and running successfully."); } catch(ArithmeticException e) {} } } Output: Compile and running successfully.

Whenever we are writing finally block compulsory we should write a try. i.e., finally without try is invalid.

class Test1 { public static void main(String[] args) { try { System.out.println("Compile and running successfully."); } catch(ArithmeticException e) {} finally {} } } Output: Compile and running successfully.

class Test1 { public static void main(String[] args) { try {} finally {} finally {} } } Output: Compile time error. Test1.java: 7 : 'finally' without 'try' Finally

In try-catch-finally order is important.

Within the try-catch-finally blocks, we can take try-catch-finally. i.e., nesting of try-catch-finally is possible.

For try-catch-finally blocks curly braces are mandatory.

What is the difference between ClassNotFoundException and NoClassDefFoundError in java?

ClassNotFoundException: An exception that occurs when you try to load a class at run time using Class.forName() or loadClass() methods and mentioned classes are not found in the classpath is called ClassNotFoundException.

NoClassDefFoundError: An exception that occurs when a particular class is present at compile-time but was missing at run time is called NoClassDefFoundError.

you can refer this post for detailed examples.

Post/Questions related to Java Exception Interview Questions and Answers


Java try-catch Examples
Try with Resources statement in Java
Checked and Unchecked Exceptions in Java

In this article, we have seen Java Exception Interview Questions and Answers.