[SOLVED] COMP90041 Lab 10-Exception handling

35.00 $

Programming resource
Digital learning resource
Category:
✓ Practical programming resource
✓ Suitable for guided study and reference
✓ Tutor guidance available when needed

Description

5/5 - (1 vote)

 

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

Resource details

Understand the Task Before You Use the Resource

Review the requirements, identify the programming concepts involved, study the implementation and test your understanding with your own examples and modifications.