0% found this document useful (0 votes)
16 views

JAVA Program

Uploaded by

Shreya Gokhale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

JAVA Program

Uploaded by

Shreya Gokhale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

AIM: Implement Exception Handling with User Defined Exception

 For Files
 For User Input

THEORY:
Exception handling is a mechanism in programming languages that allows you to handle and
manage errors or exceptional situations that may occur during the execution of a program.
Most programming languages provide a set of built-in exceptions that cover common error
scenarios, such as file not found, division by zero, or invalid input. However, there may be
cases where these built-in exceptions do not fully capture the specific error conditions in
your application domain. In such cases, you can create your own custom exceptions, known
as user-defined exceptions.
User-defined exceptions allow you to define and raise exceptions tailored to your
application's needs, providing more descriptive and meaningful error messages. This, in
turn, can improve the readability, maintainability, and debugging process of your code.
To implement exception handling with user-defined exceptions, you typically follow these
steps:
Define a custom exception class: Create a new class that inherits from the base Exception
class or any other appropriate exception class provided by the programming language. This
custom class will represent the specific exception you want to handle in your application.
Raise the custom exception: In your code, when you encounter a situation that requires
raising an exception, raise an instance of your custom exception class. This can be done by
creating an instance of the custom exception class and using the appropriate raising
mechanism provided by the programming language.
Catch and handle the custom exception: In the appropriate catch block (or equivalent
construct in the programming language), handle the raised custom exception by taking
appropriate actions. These actions can include displaying an error message, logging the
error, performing error recovery operations, or any other necessary steps specific to your
application's requirements.
By creating and using user-defined exceptions, you can encapsulate error-handling logic
specific to your application's domain, making your code more modular, extensible, and
easier to maintain. User-defined exceptions also promote code reusability, as you can define
and use these custom exceptions across different parts of your codebase, ensuring
consistent error handling and reporting.
Additionally, user-defined exceptions can be organized into a hierarchy, where more specific
exceptions can inherit from more general ones, allowing for more granular error handling
and better code organization.
INPUT:
//Model.Teachers Package
//Teacher.java
package Model.Teachers;
import Model.Subjects.Subject;
import java.util.ArrayList;
import java.util.List;
public class Teacher {
private int teacherId;
private String name;
private String emailId;
private long mobileNumber;
private double teacherRating;
private String address;
private List<Subject> subjects;
public Teacher(int teacherId, String name, String emailId, long mobileNumber, double
teacherRating, String address) {
this.teacherId = teacherId;
this.name = name;
this.emailId = emailId;
this.mobileNumber = mobileNumber;
this.teacherRating = teacherRating;
this.address = address;
this.subjects = new ArrayList<>();
}
public int getTeacherId() {
return teacherId;
}
public String getName() {
return name;
}
public String getEmailId() {
return emailId;
}
public long getMobileNumber() {
return mobileNumber;
}
public double getTeacherRating() {
return teacherRating;
}
public String getAddress() {
return address;
}
public List<Subject> getSubjects() {
return subjects;
}
@Override
public String toString() {
return "Teacher{" +
"teacherId=" + teacherId +
", name='" + name + '\'' +
", emailId='" + emailId + '\'' +
", mobileNumber=" + mobileNumber +
", teacherRating=" + teacherRating +
", address='" + address + '\'' +
'}';
}
}
//Model.Subjects Package
//Subject.java
package Model.Subjects;
import Model.Teachers.Teacher;
import java.util.ArrayList;
import java.util.List;
public class Subject {
private int subjectId;
private String subjectName;
private int subjectDuration;
private int subjectCredits;
private List<Teacher> teachers;
public Subject(int subjectId, String subjectName, int subjectDuration, int subjectCredits) {
this.subjectId = subjectId;
this.subjectName = subjectName;
this.subjectDuration = subjectDuration;
this.subjectCredits = subjectCredits;
this.teachers = new ArrayList<>();
}
public int getSubjectId() {
return subjectId;
}
public String getSubjectName() {
return subjectName;
}
public int getSubjectDuration() {
return subjectDuration;
}
public int getSubjectCredits() {
return subjectCredits;
}
public List<Teacher> getTeachers() {
return teachers;
}
@Override
public String toString() {
return "Subject{" +
"subjectId=" + subjectId +
", subjectName='" + subjectName + '\'' +
", subjectDuration=" + subjectDuration +
", subjectCredits=" + subjectCredits +
'}';
}
}
//Main.java
import java.util.InputMismatchException;
import java.util.Scanner;
import Model.Teachers.Teacher;
import Model.Subjects.Subject;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Teacher> teachers = new ArrayList<>();
List<Subject> subjects = new ArrayList<>();
try {
// Create and add teachers
Teacher teacher1 = new Teacher(1, "John Doe", "[email protected]",
1234567890, 4.5, "123 Main St");
Teacher teacher2 = new Teacher(2, "Jane Smith", "[email protected]",
9876543210, 4.8, "456 Elm St");
teachers.add(teacher1);
teachers.add(teacher2);
// Create and add subjects
Subject subject1 = new Subject(1, "Mathematics", 120, 4);
Subject subject2 = new Subject(2, "English", 90, 3);
subjects.add(subject1);
subjects.add(subject2);
// Assign teachers to subjects (many-to-many relationship)
teacher1.getSubjects().add(subject1);
teacher1.getSubjects().add(subject2);
teacher2.getSubjects().add(subject1);
subject1.getTeachers().add(teacher1);
subject1.getTeachers().add(teacher2);
subject2.getTeachers().add(teacher1);
// Print teacher and subject details
for (Teacher teacher : teachers) {
System.out.println("Teacher: " + teacher);
System.out.println("Subjects: " + teacher.getSubjects());
}
for (Subject subject : subjects) {
System.out.println("Subject: " + subject);
System.out.println("Teachers: " + subject.getTeachers());
}
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
OUTPUT:

You might also like