CoreJava - Exception Handling - Assignment
Core Java Assignments - Exception Handling
- Write a Java program that takes two integers as input and divides the first number by the second. Handle the "ArithmeticException" that occurs when dividing by zero.
2. Array Index Out of Bounds Exception:
- Create an array and try to access an element at an index that is out of bounds. Catch and handle the "ArrayIndexOutOfBoundsException."
3. Custom Exception:
- Define a custom exception class, e.g., "NegativeNumberException." Write a program that takes an integer as input and throws this exception if the number is negative.
4. File Handling Exception:
- Write a program that attempts to open a non-existent file for reading using FileReader. Handle the "FileNotFoundException."
5. Multiple Exceptions:
- Write a program that performs various operations (e.g., arithmetic, array access) and handles multiple exceptions like "ArithmeticException" and "ArrayIndexOutOfBoundsException."
6. Nested Exception Handling:
- Create a program with nested try-catch blocks, where an exception thrown in an inner try block is caught and handled in the outer catch block.
7. Finally Block Usage:
- Write a program where you open a resource (e.g., a file) in a try block and close it in the finally block. Ensure the resource is always closed, even if an exception occurs.
8. Propagate Exception:
- Define a method that takes an integer as input and throws an exception if the input is less than 10. Call this method from another method and handle the exception there.
9. Chained Exceptions:
- Create a program that demonstrates the use of chained exceptions (using the
initCause
method) to provide more context when handling exceptions.
10. Exception Propagation in Threads:
- Write a program that spawns multiple threads, and each thread may throw an exception. Demonstrate how exceptions are propagated in multi-threaded programs.
1.
Create a method that takes two integer arguments
and returns their quotient. Handle the exception that occurs if the second
argument is zero.
2.
Write a program that reads a file from the user
and displays its contents. Handle the exception that occurs if the file is not
found.
3.
Create a method that reads a double from the
user and returns its square root. Handle the exception that occurs if the input
is negative.
4.
Write a program that reads two integers from the
user and prints their sum. Handle the exception that occurs if the input is not
an integer.
5.
Create a method that takes a string as input and
returns its length. Handle the exception that occurs if the input is null.
6.
Write a program that reads a file from the user
and writes its contents to a new file. Handle the exceptions that occur if the
input file is not found or the output file cannot be created.
7.
Create a method that takes an array of integers
and returns the sum of its elements. Handle the exception that occurs if the
array is null.
8.
Write a program that reads a number from the
user and displays its reciprocal. Handle the exception that occurs if the input
is zero.
9.
Create a method that takes a string as input and
returns its first character. Handle the exception that occurs if the string is
empty.
10.
Write a program that reads a list of integers
from the user and prints their average. Handle the exception that occurs if the
list is empty.
Write code for the following scenarios :
1.
Bank Account:
Create a BankAccount class with a deposit method and a withdraw method that
take a double as input. The deposit method should add the input amount to the
account balance, and the withdraw method should subtract the input amount from
the account balance. Handle the exception that occurs if the withdrawal amount
is greater than the account balance.
2.
Password Checker:
Create a PasswordChecker class with a checkPassword method that
takes a string as input. The method should check if the input string is a valid
password by ensuring that it is at least 8 characters long and contains at
least one uppercase letter, one lowercase letter, and one digit. Handle the
exception that occurs if the input string does not meet these requirements.
3.
File Converter: Create a FileConverter class with a convertFile
method that takes two strings as input: the path to the input file and the path
to the output file. The method should read the input file, convert its contents
to uppercase, and write the result to the output file. Handle the exceptions
that occur if the input file is not found or if the output file cannot be created.
4.
Temperature
Converter: Create a
TemperatureConverter class with a convertToFahrenheit method and a
convertToCelsius method that take a double as input. The convertToFahrenheit
method should convert the input temperature from Celsius to Fahrenheit, and the
convertToCelsius method should convert the input temperature from Fahrenheit to
Celsius. Handle the exception that occurs if the input temperature is below
absolute zero.
5.
Online Store: Create an OnlineStore class with a checkout
method that takes an array of Product objects and a PaymentMethod object as
input. The checkout method should calculate the total cost of the products,
subtract the payment amount from the total cost, and return the change. Handle
the exceptions that occur if the product array is empty or if the payment
amount is less than the total cost.
Comments
Post a Comment