COMP2247 - Algorithms and Data Structures

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Exception Handling Practice1.java QuadraticEquation.java YouthLeague.java HandleTimeException Program
InvalidHourException.java
InvalidMinuteException.java
InvalidSecondException.java
HandleTimeException.java
HardwareStore Programs



HandleTimeException Program


Description

This program uses the programmer-defined exception classes to handle time exceptions.

Associated Concepts:

Exception Handling

Algorithm:

main method:

Input an integer: hours, for a time in hour.

If the user entered an invalid integer, the try block terminates immediately, and the program execution jumps to the matching catch block to handle the InputMismatchException.

If the user input is less than 0 or greater 12, the program throws an InvalidHourException which is a programmer-defined exception type.

Input an integer: minutes, for a time in minute.

If the user entered an invalid integer, the try block terminates immediately, and the program execution jumps to the matching catch block to handle the InputMismatchException.

If the user input is less than 0 or greater 59, the program throws an InvalidMinuteException which is a programmer-defined exception type.

Input an integer: seconds, for a time in second.

If the user entered an invalid integer, the try block terminates immediately, and the program execution jumps to the matching catch block to handle the InputMismatchException.

If the user input is less than 0 or greater 59, the program throws an InvalidSecondException which is a programmer-defined exception type.

The program prints the time in HH:MM:SS format if no exception occurred.

Source Code









Sample Run



Video