0% found this document useful (0 votes)
3 views1 page

Java Exercise10 - Exception Handling

The document outlines a Java exercise focused on exception handling, presenting seven tasks that require writing programs to manage various exceptions such as ArithmeticException, NumberFormatException, and custom exceptions like InvalidAgeException and InsufficientBalanceException. Each task involves user input and appropriate error handling techniques to ensure robust program behavior. The exercises cover scenarios including division operations, array access, and class attribute validation.

Uploaded by

wuxizinapefu86
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Java Exercise10 - Exception Handling

The document outlines a Java exercise focused on exception handling, presenting seven tasks that require writing programs to manage various exceptions such as ArithmeticException, NumberFormatException, and custom exceptions like InvalidAgeException and InsufficientBalanceException. Each task involves user input and appropriate error handling techniques to ensure robust program behavior. The exercises cover scenarios including division operations, array access, and class attribute validation.

Uploaded by

wuxizinapefu86
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Exercise10 [Exception Handling]

1. [ArithmeticException] Write a Java program that asks the user to enter two numbers.
Divide the first number by the second and handle the error if the user attempts to divide by zero.
Finally, display an appropriate error message.
2. [NumberFormatException] Write a Java program that asks the user to enter an integer. If
the user enters a non-numeric value, catch the error and display an appropriate error message to
the user.
3. [ArithmeticException] Write a Java program that accepts two numbers from user and
pass these numbers into another method called divide(). Create a method that performs division
and check if an exception occurs in the method and rethrow it to the calling method and handle
it there.
4. Write a Java program that asks the user to enter an index number and a number to divide. Create
an array initializer of 5 elements and access array element at given index and divide it by the
given number. Use multiple exception handling mechanism to handle array index out of bounds,
division by zero and the input type doesn’t match the expected type. Finally, report an
appropriate message to the user. [ArrayIndexOutOfBoundsException,
ArithmeticException and InputMismatchException]
5. [IllegalArgumentException] Create a class Circle with attribute radius, get method and
set method. Write a Java program that accepts radius of a circle from user and the set method
checks whether radius is negative or not. If it is negative the set method throws the exception
to caller method and handle the exception properly in the calling method and display an
appropriate error message.
6. Create a custom exception class called InvalidAgeException and ask the user to enter
age. If age is less than 18, throw the custom exception and display appropriate message.
7. Create a class Bank with attribute balance to hold the balance in the bank account, methods
deposit() and withdraw() to increase and decrease the balance respectively. Write a program
that asks the user to enter an amount to deposit to and withdraw from the account and throw
exception if withdrawal amount exceeds balance and handle invalid inputs. Create your own
custom exception class called InsufficientBalanceException.

You might also like