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



Practice1 Program


Description

This program inputs three integers: numerator, denominator, and index of an array, then uses exception handling techniques to handle several possible exceptions: InputMismatchException, ArithmeticException, and ArrayIndexOutOfBoundsException.

If no exception occurred, the program prints the quotient, the remainder, and the addition stored at the given array index.

Associated Concepts:

Exception Handling

Algorithm:

main method:

Input three integers, numerator, denominator, and index of an array,

If no exception occurred, the program prints the quotient, the remainder, and the addition stored at the given array index.

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 entered zero as the denominator, the try block terminates immediately, and the program execution jumps to the matching catch block to handle the ArithmeticException.

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

The last catch block is to use the superclass object Exception to handle any unexpected exceptions.

The finally block is executed regardless.

Source Code



Sample Run



Video