Lab Exercise 5
Exception handling
1. Write a Java program that reads a list of numbers from an array and throws an exception if
any of the numbers are positive
2. Write a Java a method that takes a string as input and throws an exception if the string does
not contain vowels
3. Write a Java program that calculates the square root of a given number entered by the user.
Use the [Link]() method to calculate the square root. However, handle the following
scenarios gracefully:
A. If the user enters a negative number, print an error message indicating that square root
of negative numbers is not defined.
B. If the user enters a non-numeric input, print an error message indicating that only
numeric inputs are allowed.
4. Simple Calculator Program: This exercise focuses on the use of Exceptions to catch a
variety of errors that can occur, allowing your program to take appropriate corrective action.
A. Implement a simple calculator program that allows the user to specify an operator and
two operands (arguments or parameters).
B. Parse these inputs, perform the operation and print out the result.
C. If an error occurs during any of these steps, such as division by zero, your program
should catch the errors with catch- try block and provide appropriate feedback to the
user. The operations of the simple calculator could be
i. Addition: Combining two numbers to find their sum
ii. Subtraction: Finding the difference between two numbers
iii. Multiplication: Calculating the product of two numbers
iv. Division: Dividing the first number by the second number to find the quotient.
v. Modulus: calculating the remainder of the division of first number by the second
vi. Exponentiation: Raising first number to the power of the second
5. User defined Exception:
A. Define a new exception, called ExceptionLineTooLong, that prints out the error
message "The name is too long".
i. Write a program that reads an input from the user and throws an exception of
typeExceptionLineTooLong in the case where the input string is longer than 15
characters.
ii. Handle also all exceptions that could be thrown by the program
B. Define another custom exception calss called OddNumberException
i. Provide two types of constructors for the OddNumberException class:
1. The first constructor doesn't take any arguments and sets a default exception
message
2. The second constructor allows you to specify a custom message for the
exception
ii. Write a Java program with a method that takes an integer as a parameter and throws
a custom exception called OddNumberException if the number is odd. Program
should handle this exception and print a message indicating whether the number is
even or odd.