Monday, December 30, 2019

Exercise – 9 (User Defined Exception)

Exercise – 9 (User Defined Exception)

a). Write a JAVA program for creation of illustrating throw


public class TestThrow1
{  
    void validate()
    {  
try
{
                System.out.println("Inside of the try block");
throw new ArithmeticException("Not  valid,throwing Exception");
}
catch(ArithmeticException ae)
{
System.out.println(ae);
}
    }  
   public static void main(String args[])
   {  
      TestThrow1 t=new TestThrow1();
t.validate();
   }  
}  

b). Write a JAVA program for creation of Illustrating finally

class TestFinallyBlock
{  
  public static void main(String args[])
  {  
  try
  {  
   int data=25/5;  
   System.out.println(data);  
   }  
  catch(NullPointerException e)
  {
System.out.println(e);
  }  
  finally
  {
System.out.println("finally block is defaultly  executed");
   }  
  System.out.println("rest of the code...");  
 }  


c). Write a JAVA program for creation of Java Built-in Exceptions



import java.util.Scanner;
class Demo
{
  public static void main(String args[])
{
ArrayIndex_Demo  obj=new ArrayIndex_Demo();
obj.display();
}
}
class ArrayIndex_Demo
        void display()
        {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the element you want to insert:");
        int n=sc.nextInt();
        System.out.print("Enter the index of the array:");
        try
       
            int a[] = new int[5]; 
            int i=sc.nextInt();
            a[i]=n;
       
        catch (ArrayIndexOutOfBoundsException e)
       
            System.out.println("Array Index is Out Of Bounds"); 
        }
        } 

d).Write a JAVA program for creation of User Defined Exception


import java.lang.Exception;
class Myexception extends Exception 
{
private static double bal[]={10000.00,12000.00,5600.50,999.00,1100.55};
Myexception(String str)
{
super(str);
}
public static void main(String args[])
{
try
{
for(int i=0;i<=5;i++)
{
System.out.println(bal[i]);
if(bal[i]<1000)
{
Myexception me=new Myexception("This is user defined Error.");
throw me;
}
}
}
catch(Myexception me)
{
me.printStackTrace();
}
}
}

No comments:

Post a Comment

JAVA PROGRAMMING ASSIGNMENT 2 QUESTIONS

SIR C R REDDY COLLEGE OF ENGINEERING, ELURU DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING JAVA PROGRAMMING ASSIGNMENT 2 QUESTIONS ...