Starting from:

$29.99

COMP90041 Lab 10 Solution


Workshop Instructions
Exception handling


1. Write a Java program that prompts the user for two integers. Use a try/catch block to handle the InputMismatchException.

2. Define an Exception class called NegativeNumberException. The class should have a constructor with no parameters. If an exception is thrown with this zero-argument constructor, the getMessage() method should return “Negative Number Not Allowed!” This class should also have a construction with a single parameter of type String. If an exception is thrown with this construction, then the getMessage() method returns the value that was used as an argument to the constructor.

3. Revise the program in Exercise 1 above to throw a NegativeNumberException if the user enters a negative number.

4. Revise the program in Exercise 3 above to allow users to try again until all the integers are valid.

Page 1 of 1

More products