Custom Exception Classes java In Hindi
जावा प्रोग्रामिंग में जावा डेवलपर डिज़ाइन डेवलप सॉफ्टवेयर एप्लिकेशन में कुछ पर्टिकुलर स्पेसिफिक कंडीशन सिनेरियो को मैनेज या डील करने के लिए एक कस्टम एक्सेप्शन (जिसे जावा यूज़र-क्रिएटेड या डिफाइंड एक्सेप्शन के रूप में भी जाना जाता है) जरूरत के अनुसार क्रिएट कर सकते हैं. जो की जावा बिल्ट-इन लाइब्रेरी एक्सेप्शन पार्ट में कवर नहीं होते हैं। याद रहे, जावा प्रोग्राम क्रिएटेड एक कस्टम एक्सेप्शन जावा यूजर क्रिएटेड एक नई क्लास होती है. जो मौजूदा प्रोग्राम में या तो Exception (चेक्ड एक्सेप्शन के लिए) या RuntimeException (अनचेक्ड एक्सेप्शन के लिए) को कांसेप्ट मेथड को एक्सटेंड करता है।

Rules for creating a custom exception in Java.
- किसी भी जावा प्रोग्राम में एक्सेप्शन क्लास या RuntimeException क्लास को जरूरत के अनुसार एक्सटेंड करके एक कस्टम एक्सेप्शन क्लास को डिफाइन या क्रिएट किया जा सकता है।
- मौजूदा जावा प्रोग्राम में ऐसे कंस्ट्रक्टर प्रोवाइड करे, जो जावा यूजर को मैसेज या कॉज़ जैसे, बिल्ट-इन एक्सेप्शन पास करने में हेल्प करे।
- सामान्य रूप से एक कस्टम क्लास एक्सेप्शन बिहेवियर के लिए toString() या printStackTrace() जैसे फंक्शन मेथड कांसेप्ट को ओवरराइड करें।
Creating a Custom Checked Exception in Java.
जावा प्रोग्राम में चेक्ड एक्सेप्शन एक ऐसा प्रोग्राम एक्सेप्शन या एरर होता है, जिसे प्रोग्राम मेथड के सिग्नेचर पोरशन में डिक्लेयर या डिफाइन किया जाना चाहिए या प्रोग्राम में डिफाइन फंक्शन मेथड में हैंडल किया जाना चाहिए। ये सभी स्टेप एक एक्सेप्शन Exception क्लास को प्रोग्राम में एक्सटेंड करते हैं।
Example of a Java Custom Checked Exception.
InsufficientCourseException (Checked Exception) explanation.
// here we define a Custom checked exception class
public class InsufficientCourseException extends Exception {
// here we define a class Constructor that takes a custom error message
public InsufficientCourseException (String message) {
super(message); // here it Pass the message to the superclass (Exception) class
}
}
In the above example.
- यहाँ इस एक्साम्प्ल में हम एक कस्टम checked exception InsufficientCourseException क्रिएट करते हैं, जो मौजूदा प्रोग्राम में Exception क्लास को एक्सटेंड करता है। इसके साथ हम इसमें एक कंस्ट्रक्टर भी प्रोवाइड करते हैं. जो एक कस्टम एरर मैसेज को इनपुट लेता है।
Example of a custom checked exception in Java.
public class Student {
private double duration;
public Student(double duration) {
this.duration = duration;
}
// here we define Method to apply course, throws InsufficientCourseException
public void selection(double amount) throws InsufficientCourseException {
if (amount > duration) {
throw new InsufficientCourseException(“Insufficient course for selection ” + amount);
} else {
duration -= amount;
System.out.println(“Course selection successful with New duration – ” + duration);
}
}
public static void main(String[] args) {
CourseSelection course = new CourseSelection(999);
try {
course.selection(1499); // hee This will throw InsufficientCourseException error
} catch (InsufficientCourseException e) {
System.out.println(“here InsufficientCourseException caught – ” + e.getMessage());
}
}
}
In the above example.
- यहाँ इस प्रोग्राम में यदि अमाउंट duration से अधिक हो जाता है, तो यहाँ selection() मेथड एक कस्टम एक्सेप्शन InsufficientCourseException को आटोमेटिक थ्रो करता है।
- यूजर डिफाइन कस्टम एक्सेप्शन को main() मेथड में InsufficientCourseException को कैच करके और एक एरर मैसेज को प्रिंट करके हैंडल किया जाता है।
Creating an unchecked custom exception in Java.
जावा प्रोग्राम में एक अनचेक्ड एक्सेप्शन RuntimeException कांसेप्ट को एक्सटेंड करता है। जावा प्रोग्राम में डिफाइन अनचेक्ड एक्सेप्शन को मेथड सिग्नेचर में क्लियर आर्डर में डिक्लेयर या डिफाइन करने की ज़रूरत नहीं होती है। इस तरह की प्रोग्राम एक्सेप्शन का यूज़ सामान्य रूप से प्रोग्रामिंग एरर या अनएक्सपेक्टेड प्रोग्राम कंडीशन को रिप्रेजेंट करने में किया जाता है, जिन्हें किसी प्रोग्राम रनटाइम प्रोसेस के दौरान कैच किया जा सकता है।
Example of an StudentAgeException/UncheckedException in Java.
// here we create a Custom unchecked exception class
public class StudentAgeException extends RuntimeException {
// here we define a Constructor that takes a custom error message
public StudentAgeException(String message) {
super(message); // here it Pass the message to the superclass (RuntimeException) class
}
}
In this example.
- यहाँ हम एक कस्टम अनचेक्ड एक्सेप्शन StudentAgeException को क्रिएट करते हैं, जो मौजूदा प्रोग्राम में एक RuntimeException को एक्सटेंड करता है।
Example of a custom unchecked exception in Java.
public class Student {
private int age;
public Student(int age) {
if (age < 0 || age > 100) {
throw new StudentAgeException(“Invalid student age range ” + age + “. please enter age between 0 and 100.”);
}
this.age = age;
}
public static void main(String[] args) {
try {
Student student = new Student(-10); // here This will throw an StudentAgeException in program
} catch (StudentAgeException e) {
System.out.println(“here student age Exception caught – ” + e.getMessage());
}
}
}
In this example.
- यहाँ StudentAgeException प्रोग्राम में यदि स्टूडेंट उम्र नेगेटिव या 100 वर्ष से अधिक है, तो Student क्लास का कंस्ट्रक्टर StudentAgeException को प्रोग्राम में थ्रो करता है।
- क्योंकि यहाँ StudentAgeException, RuntimeException को प्रोग्राम में एक्सटेंड करता है, इस वजह से एक्सेप्शन को क्लियर आर्डर पर डिक्लेयर या कैच करने की ज़रूरत नहीं है, लेकिन यह अभी भी try-catch ब्लॉक में कैच हो जाता है।
