CS8382-OBJECT ORIENTED PROGRAMMING LABORATORY-Create user defined Exception Handling in Java
CS8382-OBJECT ORIENTED PROGRAMMING LABORATORY
CREATE A USER DEFINED EXCEPTION HANDLING IN JAVA
PROGRAM:
import java.util.Scanner;
import java.io.*;
public class Demo{
public static void main(String args[]){
try {
int ch;
System.out.println("enter\n1.arithmeticexception\n2.arrayindexOfBoundexception\n3.Nullpointerexception\n4.NumberFormtexception \n enter your choice:\n");
Scanner in=new Scanner(System.in);
ch=in.nextInt();
switch(ch)
{
case 1:
int a;
a=args.length;
System.out.println("a"+a);
int b=42/a;
break;
case 2:
int c[]={1};
c[42]=99;
break;
case 3:
String s=null;
System.out.println(s.length());
break;
case 4:
String ab;
ab= "abc";
int i=Integer.parseInt(ab);
break;
default:
System.out.println("enter vaild input....");
}
}
catch(ArithmeticException e)
{
System.out.println("divide by zero:"+e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array index oob:"+e);
}
catch(NullPointerException e)
{
System.out.println("Null pointer exception"+e);
}
catch(NumberFormatException e)
{
System.out.println("Number Formt exception"+e);
}
}
}
--------------------------------------------------------------------
output:
enter
1.arithmeticexception
2.arrayindexOfBoundexception
3.Nullpointerexception
4.NumberFormtexception
enter your choice:
1
a0
divide by zero:java.lang.ArithmeticException: / by zero
enter
1.arithmeticexception
2.arrayindexOfBoundexception
3.Nullpointerexception
4.NumberFormtexception
enter your choice:
2
array index oob:java.lang.ArrayIndexOutOfBoundsException: 42
enter
1.arithmeticexception
2.arrayindexOfBoundexception
3.Nullpointerexception
4.NumberFormtexception
enter your choice:
3
Null pointer exceptionjava.lang.NullPointerException
enter
1.arithmeticexception
2.arrayindexOfBoundexception
3.Nullpointerexception
4.NumberFormtexception
enter your choice:
4
Number Formt exceptionjava.lang.NumberFormatException: For input string: "abc"
--------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment