Java Exceptions Interview Question Answers

Q1: What is an Exception?

Exception is an error event that can happen during the execution of a program and disrupts it’s normal flow.Exception is said to be thrown whenever an exceptional event occurs in java which signals that something is not correct with the code written and may give unexpected result. An exceptional event is a occurrence of condition which alters the normal program flow. Exceptional handler is the code that does something about the exception.

Q2: What is difference between Checked and Unchecked Exception in Java ?

Main difference between Checked and Unchecked Exception lies in there handling. Checked Exception requires to be handled at compile time using try, catch and finally keywords or else compiler will flag error. This is not a requirement for Unchecked Exceptions. Also all exceptions derived from java.lang.Exception classes are checked exception, exception those which extends RuntimeException, these are known as unchecked exception in Java
E.g:SqlException,FileNotFound are checked exception and ArrayIndexOutOfBoundException, AirthematicException are runtime exceptions.

Q3: What is difference between Error and Exception?

An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These JVM errors and you can not repair them at runtime.Though error can be caught in catch block but the execution of application will come to a halt and is not recoverable. While exceptions are conditions that occur because of bad input etc.
e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving user a feedback for entering proper values etc.)

Q4: What is difference between ClassNotFoundException and NoClassDefFoundError?

ClassNotFoundException is thrown when the reported class is not found by the ClassLoader in the CLASSPATH.
NoClassDefFoundError means that class was found by the ClassLoader however when trying to load the class, it ran into an error reading the class definition.

Q5: What are the Exception Handling Keywords in Java?

There are four keywords used in java exception handling.
Throw:Throw keyword is used to throw the exception manually. It is mainly used when the program fails to satisfy the given condition and it wants to warn the application.
e.g throw (new MyCustomException("Throwing exception using throw keyword");
Throws: If the function is not capable of handling the exception then it can ask the calling method to handle it by simply putting the throws clause at the function declaration.
Try-Catch: We use try-catch block for exception handling in our code. try is the start of the block and catch is at the end of try block to handle the exceptions. We can have multiple catch blocks with a try and try-catch block can be nested also. catch block requires a parameter that should be of type Exception.
Finally: finally block is optional and can be used only with try-catch block. Since exception halts the process of execution, we might have some resources open that will not get closed, so we can use finally block. finally block gets executed always, whether exception occurrs or not.

Q6: Can a catch block exist without a try block?

No. A catch block should always go with a try block.

Q7: Can a finally block exist with a try block but without a catch?

Yes. The following are the combinations try/catch or try/catch/finally or try/finally.

Q8: What is try-with-resources in java?

In Java 7 it is introduced that we can have try with resources which means that we need not have to take care of nullifying objects in finally block.
e.g:try (BufferedReader br =
new BufferedReader(new FileReader("D:\ankit.txt"))) {
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}

Q9: What will happen to the Exception object after exception handling?

Exception object will be garbage collected.

Q10: What are the constraints imposed by overriding on exception handling?

An overriding method in a subclass may only throw exceptions declared in the parent class or children of the exceptions declared in the parent class.

Q11: Can static block throw exception?

Yes, static block can throw only Runtime exception or can use a try-catch block to catch checked exception.