JAVA USER DEFINED EXCEPTION HANDLING

AIM:
            To write a java program to handle the  exception using User Defined  Exception handling.
PROCEDURE:
1)      Start the program.
2)      Create a package sms and define the personal and marks class.
3)      Create a subpackage myexcept and define the except  class which extends the Exception class.
4)      Define getdata() and putdata() to get the input output details of personal and marks.
5)      In getdata() member function throw user defined exception myexcept if the user enters the appropriate message to the user.
6)      Save and execute the program.
7)      Exit the program.

 
SOURCE CODE:
/*SMS PACKAGE*/
/*MYEXCEPT PACKAGE*/
/*EXCEPT CLASS*/
package sms.myexcept;
import java.io.*;
public class except extends Exception
{
            public except(String msg)
            {
            super(msg);
            }
}

/*PERSONAL CLASS*/
package sms;
import sms.myexcept.*;
import java.io.*;
public class personal
{
            String name,regno,branch,address,gender;
             public void getdata() throws IOException
             {
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                        System.out.println("Enter the name:");
                        name=br.readLine();
                        System.out.println("Enter branch:");
                        branch=br.readLine();
                        System.out.println("Enter gender M/F:");
                        gender=br.readLine();
                        System.out.println("Enter the address:");
                        address=br.readLine();
            }
            public void display()throws IOException
            {
                        System.out.println("Name:"+name);
                        System.out.println("Branch:"+branch);
                        System.out.println("Gender:"+gender);
                        System.out.println("Address:"+address);
            }
}

/*MARKS CLASS*/

package sms;
import sms.myexcept.*;
import java.io.*;
public class marks extends personal
{
            int total=0,i;
            float avg;
            int m[]=new int[5];     
            public void getdata() throws IOException
            {                     
                        super.getdata();
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                        i=0;
                        again:do
                        {
                                    try
                                    {
                                                            System.out.println("Enter The Marks["+i+"]:");        
                                                            m[i]=Integer.parseInt(br.readLine());
                                                            if(m[i]>100)
                                                            {
                                                                        throw new except("Marks should be<100");
                                                            }
                                                            else
                                                            {
                                                                        total+=m[i];
                                                                        avg=(float)total/5;                                                                  
                                                            }
                                               
                                    }
                                    catch(except e)
                                    {
                                                System.out.println(e);
                                                continue again;                       
                                    }                                             
                                    i=i+1;
                        }while(i<5);
            }
            public void display()throws IOException
            {
                        System.out.println("\n**********************");
                        System.out.println("     PERSONAL DETAILS   ");
                        System.out.println("**********************");
                        super.display();
                        System.out.println("\n**********************");
                        System.out.println("     ACADEMIC DETAILS   ");
                        System.out.println("**********************");
                        for(int i=0;i<5;i++)
                        {
                        System.out.println("Marks["+i+"]:"+m[i]);
                        }
                        System.out.println("Total:"+total);
                        System.out.println("Average:"+avg);
            }
}

/*MAIN CLASS*/
import sms.*;
import sms.myexcept.*;
import java.io.*;
class exno5
{
            public static void main(String args[])throws IOException
            {
                        System.out.println("Student details");
                        System.out.println("***************");
                        marks m=new marks();
                        m.getdata();
                        m.display();
            }
}








OUTPUT:

  Student details
***************
Enter the name: 4cNOTES
Enter branch: mca
Enter gender M/F: m
Enter the address: trichy
Enter The Marks[0]:98
Enter The Marks[1]:90
Enter The Marks[2]:88
Enter The Marks[3]:990
sms.myexcept.except: Marks should be<100
Enter The Marks[3]:99
Enter The Marks[4]:81

**********************
     PERSONAL DETAILS
**********************
Name:4cNOTES
Branch:mca
Gender:m
Address:trichy


**********************
     ACADEMIC DETAILS
**********************
Marks[0]:98
Marks[1]:90
Marks[2]:88
Marks[3]:99
Marks[4]:81
Total:456
Average:91.2

0 comments: