Professional Documents
Culture Documents
Question1
import java.util.Scanner;
try
catch(NumberFormatException e)
Output1
Enter an integer: 12
Output2
Question2
import java.util.Scanner;
{
public static void main(String[] args)
array[i] = s.nextInt();
try
catch(ArrayIndexOutOfBoundsException e)
System.out.println(e.getClass());
Output1
20
90
Output2
20
90
class java.lang.ArrayIndexOutOfBoundsException
Question3
import java.util.InputMismatchException;
import java.util.Scanner;
try
array[i] = scan.nextInt();
}
catch(ArrayIndexOutOfBoundsException e)
System.out.println(e);
catch(InputMismatchException e)
System.out.println("java.lang.NumberFormatException");
Output1
50
80
Output2
50
80
java.lang.ArrayIndexOutOfBoundsException:
Output3
30
java.lang.NumberFormatException
Question4
if(args.length == 5)
int sum = 0;
double avg = 0;
try
array[i] = Integer.parseInt(args[i]);
sum += array[i];
catch(Exception e){
System.out.println(e);
else
}
}
Output
Question5
import java.util.Scanner;
int n1 = scan.nextInt();
int result = 0;
int n2 = scan.nextInt();
try
result = n1/n2;
catch(Exception e)
System.out.println(e);
Output
Division is 2
Question6
import java.util.Scanner;
public NegativeValuesException()
System.out.println("NegativeValuesException occured");
public ValuesOutOfRangeException()
System.out.println("ValuesOutOfRangeException occured");
int subA = 0;
int subB = 0;
int subC = 0;
try {
name = scan.nextLine();
if (scan.hasNextInt())
subA = scan.nextInt();
else
if (scan.hasNextInt())
subB = scan.nextInt();
else
if (scan.hasNextInt())
subC = scan.nextInt();
else
catch (ArithmeticException e)
System.out.println(e.getMessage());
catch (NegativeValuesException e)
System.out.println(e.getMessage());
catch (ValuesOutOfRangeException e)
System.out.println(e.getMessage());
}
}
Question7
import java.util.Scanner;
public InvalidCountryException()
System.out.println("InvalidCountryException occured");
if (!userCountry.equals("India"))
else
name = scan.nextLine();
countryName = scan.nextLine();
try
registration.registerUser(name, countryName);
}
catch (InvalidCountryException e)
System.out.println(e.getMessage());
Output1
InvalidCountryException occured
Output2
Question8
public InvalidAgeException()
System.out.println("Invalid age");
Question9
import java.util.Scanner;
try {
int a = sc.nextInt();
int b = sc.nextInt();
int quotient = a / b;
catch (ArithmeticException e)
System.out.println("DivideByZeroException caught");
finally
Output1
The quotient of 5 / 2 = 2
DivideByZeroException caught