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

Java Exception Questions

Uploaded by

aj.ajay02nd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java Exception Questions

Uploaded by

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

1.

Write a Java program to create a method that takes an integer as a


parameter and throws an exception if the number is odd.
2. Write a Java program to create a method that reads a file and throws
an exception if the file is not found.

3. Write a Java program that reads a list of integers from the user and
throws an exception if any numbers are duplicates.

4. Create a simple banking application where users can withdraw


money from their accounts. Implement a method withdraw that
takes an amount to withdraw and the account balance. If the amount
to withdraw is greater than the balance, throw a custom exception
called InsufficientFundsException. Handle this exception in the main
method and print an appropriate message.

5. Implement a method convertToCelsius that takes a temperature in


Fahrenheit and converts it to Celsius. If the input is not a valid
number, throw a custom exception called
InvalidTemperatureException. Handle this exception in the main
method and print an appropriate message.

6. Create a Java program that performs the following tasks: (for array of
size 5)

a) Prompt the user to enter a number.


b) Calculate the square root of the number.
c) Store the result in an array of doubles.
d) Allow the user to retrieve the stored result by entering the
index.
e) Handle potential exceptions such as:
a. Non-numeric input
b. Negative numbers (for square root)
c. Array index out of bounds

You might also like