What will be the output of the following program? Javaclass CustomException…

2013

What will be the output of the following program?

Java
class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

public class Geeks {
    public static void main(String[] args) {
        try {
            throw new CustomException("Custom error occurred");
        } catch (CustomException e) {
            System.out.println(e.getMessage());
        }
    }
}


  1. A.

    Custom error occurred

  2. B.

    Runtime Exception

  3. C.

    Compilation Error

  4. D.

    No Output

Attempted by 80 students.

Show answer & explanation

Correct answer: A

The code throws a CustomException with the message 'Custom error occurred'. The catch block captures this exception and prints e.getMessage(), which returns the string passed to the constructor. Thus, the output is 'Custom error occurred'.

Explore the full course: Isro