Interview

10 Java Exception Interview Questions and Answers

Prepare for your Java interview with our guide on Java Exception handling. Enhance your skills and confidence with curated questions and answers.

Java Exception handling is a critical aspect of Java programming, enabling developers to manage runtime errors and maintain the normal flow of application execution. Mastery of exception handling is essential for writing robust, error-free code and is a key skill that employers look for in Java developers. Understanding the hierarchy of exceptions, the use of try-catch blocks, and the creation of custom exceptions are fundamental components of this topic.

This article provides a curated selection of interview questions focused on Java Exception handling. Reviewing these questions will help you deepen your understanding of exception mechanisms, improve your problem-solving abilities, and enhance your readiness for technical interviews.

Java Exception Interview Questions and Answers

1. How do you handle exceptions in Java?

In Java, exceptions are managed using try-catch blocks, the finally block, and the throws keyword. The try block contains code that might throw an exception, while the catch block handles it. The finally block executes regardless of whether an exception was thrown. The throws keyword indicates that a method might throw certain exceptions.

Example:

public class ExceptionHandlingExample {
    public static void main(String[] args) {
        try {
            int result = divide(10, 0);
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.out.println("Exception caught: " + e.getMessage());
        } finally {
            System.out.println("This block always executes.");
        }
    }

    public static int divide(int a, int b) throws ArithmeticException {
        return a / b;
    }
}

In this example, the divide method might throw an ArithmeticException if division by zero occurs. The try block attempts to call this method, and if an exception is thrown, the catch block handles it by printing an error message. The finally block executes regardless of whether an exception was thrown.

2. What is the difference between checked and unchecked exceptions?

Exceptions in Java are categorized into checked and unchecked exceptions.

Checked exceptions are verified at compile-time and must be either caught or declared in the method signature. Examples include IOException and SQLException. They enforce error handling in the code.

Unchecked exceptions, subclasses of RuntimeException, are not checked at compile-time. Examples include NullPointerException and ArrayIndexOutOfBoundsException. They typically represent programming errors and are often not recoverable.

3. Write a method that throws a custom checked exception.

Custom exceptions allow developers to create specific error types for more meaningful error messages. To create a custom checked exception, extend the Exception class.

Example:

// Define the custom checked exception
public class CustomCheckedException extends Exception {
    public CustomCheckedException(String message) {
        super(message);
    }
}

// Method that throws the custom checked exception
public class ExceptionDemo {
    public void riskyMethod() throws CustomCheckedException {
        boolean errorOccurred = true; // Simulating an error condition
        if (errorOccurred) {
            throw new CustomCheckedException("An error occurred in riskyMethod");
        }
    }

    public static void main(String[] args) {
        ExceptionDemo demo = new ExceptionDemo();
        try {
            demo.riskyMethod();
        } catch (CustomCheckedException e) {
            System.out.println(e.getMessage());
        }
    }
}

4. Write a method that demonstrates the use of multiple catch blocks.

Multiple catch blocks handle different types of exceptions that may be thrown within a try block, allowing for more granular error handling.

Example:

public class MultipleCatchExample {
    public static void main(String[] args) {
        try {
            int[] numbers = {1, 2, 3};
            System.out.println(numbers[5]); // Throws ArrayIndexOutOfBoundsException
            int result = 10 / 0; // Throws ArithmeticException
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Array index is out of bounds: " + e.getMessage());
        } catch (ArithmeticException e) {
            System.out.println("Arithmetic error: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("General exception: " + e.getMessage());
        }
    }
}

5. Write a method that uses try-with-resources to manage resources.

Try-with-resources ensures each resource is closed at the end of the statement. A resource is an object that must be closed after use, such as InputStream or OutputStream.

Example:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileManager {
    public void readFile(String filePath) {
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, BufferedReader and FileReader are resources that are automatically closed at the end of the try block, reducing the risk of resource leaks.

6. How would you log exceptions in a Java application?

Logging exceptions helps diagnose and troubleshoot issues. Using a logging framework like Log4j, SLF4J, or java.util.logging is recommended.

Example using Log4j:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ExceptionLogger {
    private static final Logger logger = LogManager.getLogger(ExceptionLogger.class);

    public static void main(String[] args) {
        try {
            int result = 10 / 0;
        } catch (ArithmeticException e) {
            logger.error("An arithmetic exception occurred: ", e);
        }
    }
}

In this example, Log4j logs an ArithmeticException, providing detailed information about the error.

7. How do you create a custom runtime exception?

Custom runtime exceptions are created by extending the RuntimeException class, allowing developers to define specific error types.

Example:

public class CustomRuntimeException extends RuntimeException {
    public CustomRuntimeException(String message) {
        super(message);
    }
}

public class Main {
    public static void main(String[] args) {
        try {
            throw new CustomRuntimeException("This is a custom runtime exception");
        } catch (CustomRuntimeException e) {
            System.out.println(e.getMessage());
        }
    }
}

8. What are some best practices for exception handling in Java?

Exception handling best practices include:

  • Use Specific Exceptions: Catch the most specific exception first for better readability.
  • Avoid Catching Generic Exceptions: Catching generic exceptions like Exception or Throwable can obscure real issues.
  • Use Finally Block: Release resources like file handles and database connections.
  • Log Exceptions: Use a logging framework to log exceptions for diagnosing issues.
  • Throw Custom Exceptions: Provide more context about errors with custom exceptions.
  • Document Exceptions: Use JavaDoc to document exceptions a method can throw.
  • Handle Exceptions at the Right Level: Handle exceptions at the appropriate level in the application.
  • Use Try-With-Resources: Automatically close resources with try-with-resources.

9. How do you create and use custom exception hierarchies?

Custom exception hierarchies are created by extending the Exception class or its subclasses, organizing error handling.

Example:

// Base custom exception
class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

// Derived custom exception for specific error type
class SpecificException extends CustomException {
    public SpecificException(String message) {
        super(message);
    }
}

// Another derived custom exception for a different error type
class AnotherSpecificException extends CustomException {
    public AnotherSpecificException(String message) {
        super(message);
    }
}

public class ExceptionHierarchyExample {
    public static void main(String[] args) {
        try {
            throw new SpecificException("This is a specific exception");
        } catch (SpecificException e) {
            System.out.println(e.getMessage());
        } catch (CustomException e) {
            System.out.println("Caught a custom exception");
        }
    }
}

In this example, CustomException is the base class for all custom exceptions. SpecificException and AnotherSpecificException are derived classes representing specific errors.

10. How do you handle exceptions in Java Streams API?

Handling exceptions in Java Streams API can be challenging because functional interfaces do not allow checked exceptions. Strategies include:

  • Wrap checked exceptions in unchecked exceptions: Wrap the checked exception in a RuntimeException or a custom unchecked exception.
  • Use a custom wrapper method: Create a utility method that wraps the functional interface and handles exceptions internally.
  • Collect exceptions separately: Collect exceptions separately and handle them after stream processing.

Example:

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ExceptionHandlingInStreams {
    public static void main(String[] args) {
        List<String> data = Arrays.asList("1", "2", "a", "3");

        List<Integer> result = data.stream()
            .map(handleException(Integer::parseInt, -1))
            .collect(Collectors.toList());

        System.out.println(result); // Output: [1, 2, -1, 3]
    }

    private static <T, R> Function<T, R> handleException(FunctionWithException<T, R> function, R defaultValue) {
        return i -> {
            try {
                return function.apply(i);
            } catch (Exception e) {
                return defaultValue;
            }
        };
    }

    @FunctionalInterface
    public interface FunctionWithException<T, R> {
        R apply(T t) throws Exception;
    }
}
Previous

10 Couchbase Interview Questions and Answers

Back to Interview
Next

10 WMS Interview Questions and Answers