COMSATS University Islamabad, Wah Campus
Department of Computer Science
Program/Class: BSE 3 Date: 8 December, 2022
Subject: Object Oriented Programming Instructor: Samia Riaz
_______________________________________________________________________
Question No 1: When to use exception handling?
Question No 2: Identify abstract class/ class / interface in exception handling class hierarchy?
Question No 3: Create a new Java file and:
Create an exception class named NegativeNumberException extended from the class
Exception. This class should contain a parameterless constructor to call the Exception class
constructor with the message “You should enter a positive number”.
Create a class named exercise3 that contains:
A method named M1 that accepts a double variable as argument and returns its square root.
The method should throw the NegativeNumberException exception defined above.
The main method to prompt the user to enter a number and then to call the method M1
(insert a try-catch block to handle the NegativeNumberException exception)
Question No 4:
Write a program that calculates the average of N integers. The program should prompt the
user to enter the value for N and then afterward must enter all N numbers. If the user enters
a negative value for N, then an exception should be thrown (and caught) with the message “
N must be positive.” If there is any exception as the user is entering the N numbers, an error
message should be displayed, and the user prompted to enter the number again