0% found this document useful (0 votes)
44 views

Oop Assignment (Exception Handling)

Exception handling is used to handle errors and unexpected conditions in code. The Exception class is abstract, while classes like NegativeNumberException that extend Exception are concrete. The document provides questions and examples to practice exception handling in Java. It asks the student to create an exception class that inherits from Exception and throws it if a negative number is passed to a square root method. It also asks the student to write a program that calculates averages, handles exceptions if the user enters a negative value for N, and retries if errors occur on input.

Uploaded by

Laibaa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Oop Assignment (Exception Handling)

Exception handling is used to handle errors and unexpected conditions in code. The Exception class is abstract, while classes like NegativeNumberException that extend Exception are concrete. The document provides questions and examples to practice exception handling in Java. It asks the student to create an exception class that inherits from Exception and throws it if a negative number is passed to a square root method. It also asks the student to write a program that calculates averages, handles exceptions if the user enters a negative value for N, and retries if errors occur on input.

Uploaded by

Laibaa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

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

You might also like