What will be the output of the following program? Javaclass CustomException…
2013
What will be the output of the following program?
Javaclass 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());
}
}
}
- A.
Custom error occurred
- B.
Runtime Exception
- C.
Compilation Error
- 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'.