0% found this document useful (0 votes)
9 views4 pages

Assignment Oop6

The document contains a Java program that demonstrates exception handling for various scenarios, including ArithmeticException, InputMismatchException, ArrayIndexOutOfBoundsException, and NumberFormatException. The program prompts the user to select an error type to test and handles exceptions accordingly while providing feedback. It includes methods to perform arithmetic operations, manage array inputs, and parse integers from user input.

Uploaded by

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

Assignment Oop6

The document contains a Java program that demonstrates exception handling for various scenarios, including ArithmeticException, InputMismatchException, ArrayIndexOutOfBoundsException, and NumberFormatException. The program prompts the user to select an error type to test and handles exceptions accordingly while providing feedback. It includes methods to perform arithmetic operations, manage array inputs, and parse integers from user input.

Uploaded by

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

import java.util.

*;
class ExceptionEx
{
public void Arithmatic()
{
int num1 , num2;
int result;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Two Numbers ::");
try
{
num1=sc.nextInt();
num2=sc.nextInt();
result=num1/num2;
System.out.println("The result is "+result);
}
catch(ArithmeticException e)
{
System.out.println("Number cannot be divided by zero");
}

public void InputMismatchFormat()


{
int num1 , num2;
int result;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Two Numbers ::");
try
{
num1=sc.nextInt();
num2=sc.nextInt();
result=num1/num2;
System.out.println("The result is "+result);
}
catch(InputMismatchException e)
{
System.out.println("Enter only integer values ");
}
System.out.println(" \n successfully completed \n ");
}

public void ArrayOutOfBound()


{
int a[]= new int[5];
int i=0;
int x;
Scanner sc = new Scanner(System.in);
System.out.println("Enter 5 values :");
Scanner ss= new Scanner(System.in);
try
{
while(i<5)
{
a[i]=sc.nextInt();
i++;
}
System.out.print("\nEnter the Index No. of Element which you want
to print ::");
i=ss.nextInt();
System.out.println( a[i]+" is present at given index");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index out of Bound");
}
System.out.println(" \n successfully completed \n ");
}

public void Numberformat()


{
int number;
Scanner sc = new Scanner(System.in);
System.out.println("Enter any valid Integer: ");
try
{
number = Integer.parseInt(sc.next());
System.out.println("You entered: "+ number);

}
catch (NumberFormatException e)
{

System.out.println("NumberFormatException occurred");
}

System.out.println("\n successfully completed \n ");


}

}
public class Excep
{
public static void main(String[] args)
{
ExceptionEx e = new ExceptionEx();

Scanner sc=new Scanner(System.in);


int n;
do
{
System.out.println("\n Enter the Choice which you want to check
type of error "+"\n\t1] Arithmaticexception"+"\n\t2] ArrayOutOfBound"+"\n\t3]
NumberFormat"+"\n\t4] Input Mismatch Exception " + "\n\t5 Exit");
n=sc.nextInt();
switch(n)
{
case 1:
e.Arithmatic();
break;
case 2:
e.ArrayOutOfBound();
break;
case 3:
e.Numberformat();
break;
case 4:
e.InputMismatchFormat();
case 5:
n=0;
break;
}
}while(n!=0);
}
}

OUTPUT:

Enter the Choice which you want to check type of error


1] Arithmaticexception
2] ArrayOutOfBound
3] NumberFormat
4] Input Mismatch Exception
5 Exit
1
Enter Two Numbers ::
40
0
Number cannot be divided by zero

Enter the Choice which you want to check type of error


1] Arithmaticexception
2] ArrayOutOfBound
3] NumberFormat
4] Input Mismatch Exception
5 Exit
1
Enter Two Numbers ::
40
2
The result is 20

Enter the Choice which you want to check type of error


1] Arithmaticexception
2] ArrayOutOfBound
3] NumberFormat
4] Input Mismatch Exception
5 Exit
2
Enter 5 values :
4
5
6
1
2

Enter the Index No. of Element which you want to print ::3
1 is present at given index

successfully completed

Enter the Choice which you want to check type of error


1] Arithmaticexception
2] ArrayOutOfBound
3] NumberFormat
4] Input Mismatch Exception
5 Exit
3
Enter any valid Integer:
5.2
NumberFormatException occurred

successfully completed

Enter the Choice which you want to check type of error


1] Arithmaticexception
2] ArrayOutOfBound
3] NumberFormat
4] Input Mismatch Exception
5 Exit
3
Enter any valid Integer:
5
You entered: 5

successfully completed

Enter the Choice which you want to check type of error


1] Arithmaticexception
2] ArrayOutOfBound
3] NumberFormat
4] Input Mismatch Exception
5 Exit
4
Enter Two Numbers ::
10
20
The result is 0

successfully completed

=== Code Execution Successful ===

You might also like