Computer Science · Java Programming
What will be the output of the following Java program? class CustomException extends Exception { public CustomException(String message) { super(message); } } public class TestCustomException { public static void main(String[] args) { try { validateAge(15); } catch (CustomException e) { System.out.println('Caught Exception: ' + e.getMessage()); } finally { System.out.println('Execution finished.'); } } public static void validateAge(int age) throws CustomException { if (age < 18) { throw new CustomException('Age must be 18 or above.'); } System.out.println('Age is valid.'); } }
- 1Caught Exception: null Execution finished.
- 2Caught Exception: Age must be 18 or above. Execution finished.✓
- 3Age is valid. Execution finished.
- 4Compilation error because custom exceptions cannot extend Exception.
Correct answer
Caught Exception: Age must be 18 or above. Execution finished.
Download Single Question PDF
Save this question and verified answer as a clean study sheet with live clickable link.
Share with Friends & Study Groups
Challenge your peer aspirants on WhatsApp or Telegram to solve this question!
Preparing for Computer Science?
Practice full-length mock tests with real exam timers, instant score analysis, and all-India ranking.
Want more practice questions on Java Programming?
Solve complete topic-wise tests with detailed solutions on GkGet.
Related Practice Questions
Recommended practice questions in Computer Science · Java Programming