Milestone1-Exception Handling
Milestone1-Exception Handling
Type of
No Material Title Material Location Materia Classification
l
No Topics
Hands-on Assignment Status
. Covered
3 Exception
Handling:
Try-catch Use
multiple catch
block
Write a program that takes as input the size of the array and
the elements in the array. The program then asks the user to
enter a particular index and prints the element at that index.
Index starts from zero.
7 Exception
Handling:
A student portal provides user to register their profile. User Defined
During registration the system needs to validate the user Exception &
should be located in India. If not the system should throw an throw
exception.
Step 1: Create a user defined exception class named
“InvalidCountryException”.
Step 2: Overload the respective constructors.
Step 3: Create a main class “UserRegistration”, add the
following method,
registerUser– The parameter are String username,String
userCountry and add the following logic,
• if userCountry is not equal to “India” throw a
InvalidCountryException with the message “User Outside India
cannot be registered”
• if userCountry is equal to “India”, print the message
“User registration done successfully”
Invoke the method registerUser from the main method with the
data specified and see how the program behaves,
Name Country Expected Output
Mickey US InvalidCountryException should be thrown.
The message should be “User Outside India cannot be
registered”
Mini India The message should be “User registration done
successfully”
Sample Input and Output
9 Exception
Write a program that accepts 2 integers a and b as input and Handling:
finds the quotient of a/b. Finally block
This program may generate an Arithmetic Exception. Use
exception handling mechanisms to handle this exception. In the
catch block, print the message as shown in the sample output.
Also illustrate the use of finally block. Print the message
“Inside finally block”.
Sample Input and Output 1:
Enter the 2 numbers
5
2
The quotient of 5/2 = 2
Inside finally block
Sample Input and Output 2:
Enter the 2 numbers
5
DivideByZeroException caught
Inside finally block