Exercise - 7 (Exception)
a).Write a JAVA program that describes exception handling mechanismimport java.util.Scanner;
class Exception_handle
{
public static void main(String args[])
{
Exception ex=new Exception();
}
}
class Exception
{
Exception()
{
Scanner s=new Scanner(System.in);
System.out.print("Enter a and b values:");
int a=s.nextInt();
int b=s.nextInt();
try
{
int c=a/b;
System.out.print(c);
}
catch(ArithmeticException ae)
{
System.out.println("Arthmatic Exception occured");
}
}
}
b).Write a JAVA program Illustrating Multiple catch clauses
import java.util.Scanner;
class Multiple_catch
{
public static void main(String args[])
{
Handle h=new Handle();
}
}
class Handle
{
Handle()
{
Scanner sc=new Scanner(System.in);
String str="The world is runs throgh software industry";
String str2=null;
System.out.print("Enter the index of 1st string where you want to retrive the charecter:");
int n1=sc.nextInt();
System.out.print("Enter the index of 2st string where you want to retrive the charecter:");
int n2=sc.nextInt();
try
{
char c=str.charAt(n1);
System.out.println("Charecter at the index of "+n1+" 1st string is:"+c);
char ch=str2.charAt(n2);
System.out.println(ch);
}
catch(StringIndexOutOfBoundsException sib)
{
System.out.println("String index out of range");
}
catch(NullPointerException np)
{
System.out.println("Null pointer exception");
}
}
}
No comments:
Post a Comment