Sei sulla pagina 1di 2

Exception–Exercise

#1 Program
 Write a java program to catch the checked exception

#2 Program
 Write a java program to catch the un-checked exception

#3 Program
 Create a class with a main() method. Define an object reference and initialize it to null in a try block.
 Give the constructor for Exception aString argument. Catch the exception inside a catch clause and
print the String argument. Add a finally clause and print a message to prove you were there.

#4 Program
Write code to generate and catch an ArrayIndexOutOfBoundsException and catch the exception and
print the message.

#5 Program
Write a class with a method that throws below type of exception.

 StringIndexOutOfBoundsException,

And catch the exception in the calling method and print the custom error message.

#6 Program
Write a class with a method that throws below type of exception. Use a different catch clause that will
catch the above of exceptions.

 StringIndexOutOfBoundsException,
 NumberFormatException,
 ArithmeticException

#7 Program
Write a java program to accept a student age and print the below using the custom exception. Also print
the student age with age+1;
String temp = null;
String temp1 = temp.trim() + “ end.”

#8 Program
Write a java class that accepts a number and using the user defined exception class and print the below,
pass the value to the custom exception.

a. age <5 print “This is a baby”


b. age >5 and <10 - print “This is a Children”
c. age >10 and <25 - print “This is a Student”
d. age >25 and <50 - print “This is a Young Man”
e. If age >50 - “This is a old man”
Exception–Exercise

#9 Program
Read the data from 1 to 5 and write a user defined exception class which will take a number as
input and prints the square of this number as the output.

# 10 Program:
Create a program to accept 2 String which contains only numbers. (Print the value using the user-
defined exception, raise error message if not a number. “Please enter a valid number..!”;

a. Calculate the sum of each digits in String1 and String2 separately


b. Print “String 1 is greater” or “String 2is greater” based on the comparison

OUTPUT:
Which one is greater?________________

#11 Program
Program to read a number and print the number is whole number or fractional number. User
proper exception, of not any number then raises an error message “Please enter a valid number..!”. Use
method to do this process.

#12 Program
Write a program to read 10 numbers combination of int and double and store the data to String
array. Copy the data to int and double array based on the value in String array and print the data in
both the arrays Use proper exception to catch the invalid result and print the error message and break
the program. Print the array data only if that contains any numbers.

Potrebbero piacerti anche