Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Experiment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

//1.

Throw Exception manually program


import java.io.*;
class myexception extends Exception
{
    myexception(String msg)
    {
        super(msg);
    }
}
class myexceptiondemo
{
    public static void main(String args[])
    {
        BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
        String name;
        int age;
           
        try{
            System.out.println("Enter name");
            name=br.readLine();
            System.out.println("Enter age");
            age=Integer.parseInt(br.readLine());
           
            if(age<0)
            {
                throw new myexception("Entered  age is negative");
            }
            else
            {
                throw new myexception("Entered  age is correct");
            }
        }
        catch(myexception e)
        {
            System.out.println(e);
        }
        catch(Exception e1)
        {
            System.out.println(e1);
        }  
    }
}
/*
OUTPUT:
D:\Diploma\CO_4-I\AJAVA\practical>javac 26_27_1.java
D:\Diploma\CO_4-I\JAVA\practical>java myexceptiondemo
Enter name
abc
Enter age
19
myexception: Entered  age is correct
 
D:\Diploma\CO_4-I\JAVA\practical>java myexceptiondemo
Enter name
abc
Enter age
-1
myexception: Entered  age is negative

*/

//2 .Define an exception  NotMatchException .


import java.io.*;
class NotMatchException extends Exception
{
    NotMatchException(String msg)
    {
        super(msg);
    }
}

class myexceptiondemo2
{
    public static void main(String args[])
    {
        BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
        String s1;
        try{
            System.out.println("Enter string");
            s1=br.readLine();
           
            if(s1.equals("India"))
            {
                throw new NotMatchException("same string ");
            }          
            else
            {
                throw new NotMatchException("differnt string ");
        }
        }
       
        catch(NotMatchException e)
        {
            System.out.println(e);
                }
        catch(Exception e1)
        {
            System.out.println(e1);
        }
       
    }
}
/*
OUTPUT:
D:\Diploma\CO_4-I\JAVA\practical>javac 26_27_2.java
D:\Diploma\CO_4-I\JAVA\practical>java myexceptiondemo2
Enter string
India
NotMatchException: same string

D:\Diploma\CO_4-I\JAVA\practical>java myexceptiondemo2
Enter string
abc
NotMatchException: differnt string
*/

//2.Write simple program for throwing our own exception


 import java.io.*;
class myexception extends Exception
{
    myexception(String msg)
    {
        super(msg);
    }
}

class myexceptiondemo
{
    public static void main(String args[])
    {
        BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
        int no;
        try{
           
            System.out.println("Enter number");
            no=Integer.parseInt(br.readLine());
            if(no%2==0)
            {
                throw new myexception("no  is even");
            }
            else
            {
                throw new myexception("no  is odd");
            }
        }
        catch(myexception e)
        {
            System.out.println(e);
        }
        catch(Exception e1)
        {
            System.out.println(e1);
        }  
    }
}
/*
OUTPUT:
D:\Diploma\CO_4-I\JAVA\practical>javac 26_27_3.java
D:\Diploma\CO_4-I\JAVA\practical>java myexceptiondemo
Enter number
10
myexception: no  is even

D:\Diploma\CO_4-I\JAVA\practical>java myexceptiondemo
Enter number
9
myexception: no  is odd
*/

You might also like