All about Exception Handling

Veenarao
Javarevisited
Published in
7 min readJul 2, 2024

Dear Readers, Im writing this blog to summarise interview questions about Exception Handling.

Java Exception Handling
Java Exception Handling

An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore these exceptions are to be handled.

Java Exception Hierarchy
Java Exception Hierarchy

Here are some of the questions most frequently asked

  1. why do we need exception handling in java?
  2. Whats the difference between Exception and Error?
  3. Explain Exception Hierarchy?
  4. Different types of Exceptions?
  5. Describe the difference between unchecked and checked exceptions in Java.
  6. What is the difference between Finally, Final, and Finalize in Java?
  7. Define try-with resource. How can you say that it differs from an ordinary try?
  8. What is Runtime Exception?
  9. What is the difference between NoClassDefFoundError and ClassNotFoundException in Java?
  10. How to throw an exception explicitly or manually?
  11. Describe the use of the throw keyword.
  12. Why is finally block used?
  13. What is OutofMemoryError ?
  14. What is the error of ClassCastException?
  15. Is there any difference between throw and throws ?
  16. Important methods of Exception class?
  17. Can we have an empty catch block?
  18. Can checked exceptions occur at compiled time?
  19. What happens if a runtime exception occurs?
  20. Describe unreachable catch block error in Java.
  21. When finally block doesnt get executed?
  22. Is it possible to throw a statement inside a static block?
  23. Define user-defined or custom exceptions in Java.
  24. What is chained exception?
  25. What do you understand about throwables in Java?
  26. Mention the methods in the throwable class.
  27. what is unreachable catch block error?
  28. what is re throw?
  29. Is it possible to throw exception from lamda expressions? Answer: yes
  30. If a parent class method does not throw any exceptions, can the overridden method in the child class throw checked exceptions?

Exception Handling during function over-riding

  1. If a parent class method does not throw any exceptions, can the overridden method in the child class throw checked exceptions?
  2. When a parent class method throws checked exceptions, what types of exceptions can the child class method throw when overriding it?
  3. What will happen if an overridden method in a child class throws a checked exception that is a superclass of the exceptions thrown by the parent class method?
  4. Can a child class method throw more checked exceptions than the parent class method? Under what conditions is this allowed or not allowed?
  5. If a parent class method throws an unchecked exception, what constraints apply to the child class method in terms of the exceptions it can throw?

Output Prediction:

  1. Predict the output?
List<Integer> list = Arrays.asList(2,3,5,10,20); list.forEach(i -> {
if (i < 0) {
throw new IllegalArgumentException("Negative numbers are not allowed.");
} System.out.println(i); });

Output: If we are using custom functional interfaces, then we can throw checked as well as unchecked exceptions. Custom functional interfaces can be defined by using the @FunctionalInterface keyword.

2. Predict the output?

public class ExceptionHirerachy {
public static void main(String[] args) {
try {
demoException();
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}

public static void demoException() throws IOException, FileNotFoundException{

}
}

Output: Since the Exception ‘java.io.FileNotFoundException’ has already been caught , its throws compilation error.

3. Predict the output?

public class TestExceptionDemo {

public static void main(String[] args) {
try{
demoMethod();
}catch(NullPointerException e){
System.out.println(e.getMessage());
}catch(Exception e){
System.out.println(e.getMessage());
}

foobar();
}

public static void demoMethod(){

}

public static void foobar() throws NullPointerException{

}
}

output: nothing.

Calling demoMethod() inside try block:

  • demoMethod() is called, but it is an empty method and does not throw any exceptions.
  • Therefore, neither NullPointerException nor Exception is caught, and nothing is printed from the try-catchblock.
  • Calling foobar() after the try-catch block:
  • foobar() is called, but it is also an empty method.
  • Although foobar() has a throws NullPointerException clause, it does not actually throw an exception.
  • Therefore, no exception is thrown, and no exception handling occurs.

4.Predict the output?

class Parent {
void display() {
System.out.println("Parent display");
}
}

class Child extends Parent {
void display() throws IllegalArgumentException {
System.out.println("Child display");
}

public static void main(String[] args) {
Parent p = new Child();
p.display();
}
}

output: Since IllegalArgumentException is an unchecked exception, the Child class can override the display method without any issues. The actual method that gets called is Child's display method due to polymorphism.

5. Predict the output?

class Parent {
void show() throws IOException {
System.out.println("Parent show");
}
}

class Child extends Parent {
void show() throws FileNotFoundException {
System.out.println("Child show");
}

public static void main(String[] args) {
try {
Parent p = new Child();
p.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}

output: child show The show method in the Child class throws a FileNotFoundException, which is a subclass of IOException that the Parent class's show method throws. This is allowed and the overridden method is called.

6. Predict the output:

class Parent {
void show() throws IOException {
System.out.println("Parent show");
}
}

class Child extends Parent {
void show() throws Exception { // Compilation error
System.out.println("Child show");
}

public static void main(String[] args) {
try {
Parent p = new Child();
p.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}

output:Compilation error

The show method in the Child class cannot throw a broader checked exception (Exception) than the one thrown by the Parent class method (IOException). This will result in a compilation error.

7. Predict the output?

class Parent {
void show() {
System.out.println("Parent show");
}
}

class Child extends Parent {
void show() throws IOException {
System.out.println("Child show");
}

public static void main(String[] args) {
Parent p = new Child();
p.show();
}
}

Output: Compiler error . The show method in the Parent class does not throw any exceptions, so the overridden method in the Child class cannot throw any checked exceptions like IOException. This will result in a compilation error.

8.Predict the output?

class Parent {
void show() throws IOException {
System.out.println("Parent show");
}
}

class Child extends Parent {
void show() throws FileNotFoundException {
System.out.println("Child show");
}

public static void main(String[] args) {
try {
Child c = new Child();
c.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Output: Child Show .Here, the show method in the Child class throws a FileNotFoundException, which is a subclass of IOException. Since Child is used directly, the overridden method is called, and the exception is correctly handled by the catch block.

9.Predict the output?

class Parent {
void display() throws NullPointerException {
System.out.println("Parent display");
}
}

class Child extends Parent {
void display() throws ArithmeticException, NumberFormatException {
System.out.println("Child display");
}

public static void main(String[] args) {
Parent p = new Child();
p.display();
}
}

output: Child display.

The display method in the Parent class throws an unchecked exception (NullPointerException). The overridden display method in the Child class can throw any number of unchecked exceptions. The overridden method is called due to polymorphism.

10.Predict the output?

class Parent {
void show() throws IOException {
System.out.println("Parent show");
}
}

class Child extends Parent {
void show() throws FileNotFoundException, EOFException {
System.out.println("Child show");
}

public static void main(String[] args) {
try {
Parent p = new Child();
p.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}

output:child show

The show method in the Child class throws FileNotFoundException and EOFException, both of which are subclasses of IOException. This is allowed, and the overridden method is called and handled correctly by the catch block.

11. Predict the output?

class Test {
public static void main(String[] args) {
System.out.println(method());
}

static int method() {
try {
return 1;
} catch (Exception e) {
return 2;
} finally {
// No return statement here
System.out.println("Finally block executed");
}
}
}

output: Finally block executed. 1

The try block completes successfully and returns 1. The finally block is executed afterwards, but since it doesn't contain a return statement, it doesn't override the return value from the try block. The message "Finally block executed" is printed from the finally block.

12.Predict the output?

class Test {
public static void main(String[] args) {
System.out.println(method());
}

static int method() {
try {
throw new RuntimeException();
} catch (Exception e) {
return 2;
} finally {
return 3;
}
}
}

output:3

The try block throws a RuntimeException, which is caught by the catch block. The catch block returns 2, but the finally block contains a return statement, which overrides the return value from the catch block.

  • Thank you for reading this article. Please provide your valuable suggestions/ feedback.
  • Clap and Share if you liked the content.
  • 📰 Read more content on my Medium (on Java Developer interview questions)
  • 🔔 Follow me on : LinkedIn

Please find my other useful articles on Java Developer interview questions

Following are some of the famously asked Java8 Interview Questions

Frequently asked Java Programs

Dear Readers, these are the commonly asked java programs to check your ability on writing the logic

SpringBoot Interview Questions | Medium

Rest and Microservices Interview Questions| Medium

--

--