Exceptions
Exceptions
Tutorial- Exceptions
1. What Is an Exception in Java?
2. Why Exception Occurs? Give 4 reasons
3. Give the 2 Java Exception Categories
4. Copy the following code and run it then explain in detail on what happens. Where does it get
this message java.lang.ArithmeticException: / by zero
2.
4. try
5. {
7. }
9. catch(Exception e)
10. {
11. System.out.println(e);
12. }
14. }
15.
16. }
5. Write a Java program that prompts the user to enter their name and age, validates the input
and prints a greeting message to the console. If the user enters an invalid age, the program
shouw throw a custom exception called “InvalidInputException” the program should be
implemented as follows.
a. Create a custom exception class called “InvalidInputException” that printsout the
message “Invalid age: Age should be a positive integer less than or equal to 120” if
the age is a negative number or greater than 120
b. Create another class “GreetingValidator” to get the user user inputs through the
main method. The program should prompt the user to enter their name and age.
c. The program should validate the age by checking that it is a positive integer less than
or equal to 120. If the age is invalid, the program should throw an
InvalidInputException.
d. If both the age is valid, the program should print a greeting message to the console
that includes the user’s name and age.