0% found this document useful (0 votes)
17 views5 pages

Java 4 Shami

Uploaded by

717823l121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Java 4 Shami

Uploaded by

717823l121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY

Ex no: 4
IMPLEMENT PROGRAMS USING EXCEPTION HANDLING MECHANISMS TO ENSURE
Date: ROBUST ERROR DETECTION AND GRACEFUL RECOVERY

Question 1
Write Java programs to handle the following exceptions:
a. InputMismatchException
b. NumberFormatException
c. ArrayIndexOutofBoundsException
d. NullPointerException

AIM:
To write a java program using exception handling.

SOURCE CODE:
import java.io.*;
import java.util.*;
class ex3_1
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
try
{
String s=sc.next();
int n=Integer.parseInt(s);
}
catch(NumberFormatException e)
{
System.out.println(e);
}
try
{
int n=sc.nextInt();
}
catch(InputMismatchException e)
{
System.out.println(e);
}
try
{
int x[]=new int[5]; x[5]=10;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
try{
String str=null;
int l=str.length();
}
catch(NullPointerException e)

Register no717823L121
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY

{
System.out.println(e);
}
}

OUTPUT:

Register no717823L121
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY

Question 2:
Write a Java program based on the following statements:

. Create a CourseException class that extends Exception and whose constructor receives a String that holds a
college course’s department (for example, CIS), a course number (for example, 101), and a number of cre its
(for example, 3). Save the file as CourseException.java.

. Create a Course class with the same fields and whose constructor requires values for each field. Upon
construction, throw a CourseException if the department does not consist of three letters, if the course number
does not consist of three digits between 100 and 499 inclusive, or if the credits are less than 0.5 or more than 6.
Save the class as Course.java.

. Write an application that establishes an array of at least six Course objects with valid and invalid values.
Display an appropriate message when a Course object is created successfully and when one is not.

AIM:
To write a java program using exception handling.

SOURCE CODE:
public class CourseException extends Exception
{
public CourseException(String s)
{
super(s);
}
}
public class Course
{
private String cname;
private int cnum;
private double credits;
public Course(String cname,int cnum,double credits)
throws CourseException
{
boolean coursename=(cname.length()==3); boolean coursenum=(cnum>=100 && cnum<=499);
boolean cred=(credits>=0.5 && credits<=6.0); if(!coursename || !coursenum || !cred)
{
throw new CourseException("Invalid course details");
}
this.cname=cname;
this.cnum=cnum;
this.credits=credits;
System.out.println("Course created successfully");

}
class ex3_3{ public static void main(String[] args)
{
Course[] c=new Course[6];
try{

Register no717823L121
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY

c[0]=new Course("JPA",101,3);
c[1]=new Course("DSA",102,5.0);
c[2]=new Course("DMS",103,6.5);
c[3]=new Course("DEM",100,4.0);

c[4]=new Course("DBM",105,2.0);
c[5]=new Course("INC",106,0.2);
}
catch(CourseException e)
{
System.out.println(e);
}
}
}

OUTPUT:

RESULT:

Thus, the Java programs using exception handling mechanisms to ensure robust error detection and
graceful recovery has been successfully developed and the output is verified.

Register no717823L121
DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING 23CSR306 JAVA PROGRAMMING LABORATORY

RegisterNumber:717823L121

You might also like