जावा प्रोग्राम कस्टम अपवाद बनाने के लिए

इस उदाहरण में, हम जावा में कस्टम जाँच और अनियंत्रित अपवाद बनाना सीखेंगे।

इस उदाहरण को समझने के लिए, आपको निम्नलिखित जावा प्रोग्रामिंग विषयों का ज्ञान होना चाहिए:

  • जावा अपवाद
  • जावा एक्सेप्शन हैंडलिंग
  • जावा क्लास और ऑब्जेक्ट्स

उदाहरण 1: कस्टम जाँच अपवाद बनाने के लिए जावा प्रोग्राम

 import java.util.ArrayList; import java.util.Arrays; // create a checked exception class class CustomException extends Exception ( public CustomException(String message) ( // call the constructor of Exception class super(message); ) ) class Main ( ArrayList languages = new ArrayList(Arrays.asList("Java", "Python", "JavaScript")); // check the exception condition public void checkLanguage(String language) throws CustomException ( // throw exception if language already present in ArrayList if(languages.contains(language)) ( throw new CustomException(language + " already exists"); ) else ( // insert language to ArrayList languages.add(language); System.out.println(language + " is added to the ArrayList"); ) ) public static void main(String() args) ( // create object of Main class Main obj = new Main(); // exception is handled using try… catch try ( obj.checkLanguage("Swift"); obj.checkLanguage("Java"); ) catch(CustomException e) ( System.out.println("(" + e + ") Exception Occured"); ) ) )

आउटपुट

 स्विफ्ट को ArrayList (CustomException: Java में पहले से मौजूद है) में अपवाद जोड़ा गया है

उपरोक्त उदाहरण में, हमने Exceptionकस्टम अपवाद नामक एक कस्टम अपवाद बनाने के लिए कक्षा का विस्तार किया है । यहां, हम कीवर्ड Exceptionका उपयोग करके CustomException क्लास से क्लास के कंस्ट्रक्टर को कॉल करते हैं super()

विधि के अंदर checkLanguage(), हमने अपवाद स्थिति की जांच की है, और यदि अपवाद होता है, तो प्रयास करें … पकड़ ब्लॉक अपवाद को संभालता है।

यहाँ, यह जाँच अपवाद है। हम जावा में अनियंत्रित अपवाद वर्ग भी बना सकते हैं। चेक किए गए और अनियंत्रित अपवाद पर अधिक जानने के लिए, जावा अपवाद पर जाएँ।

उदाहरण 2: कस्टम अनियंत्रित अपवाद वर्ग बनाएँ

 import java.util.ArrayList; import java.util.Arrays; // create a unchecked exception class class CustomException extends RuntimeException ( public CustomException(String message) ( // call the constructor of RuntimeException super(message); ) ) class Main ( ArrayList languages = new ArrayList(Arrays.asList("Java", "Python", "JavaScript")); // check the exception condition public void checkLanguage(String language) ( // throw exception if language already present in ArrayList if(languages.contains(language)) ( throw new CustomException(language + " already exists"); ) else ( // insert language to ArrayList languages.add(language); System.out.println(language + " is added to the ArrayList"); ) ) public static void main(String() args) ( // create object of Main class Main obj = new Main(); // check if language already present obj.checkLanguage("Swift"); obj.checkLanguage("Java"); ) )

आउटपुट

 Swift is added to the ArrayList Exception in thread "main" CustomException: Java already exists at Main.checkLanguage(Main.java:21) at Main.main(Main.java:37)

उपरोक्त उदाहरण में, हमने RuntimeExceptionएक अनियंत्रित कस्टम अपवाद वर्ग बनाने के लिए कक्षा का विस्तार किया है ।

यहाँ, आप देख सकते हैं कि, हमने कोई प्रयास नहीं किया है ब्लॉक को पकड़ें। ऐसा इसलिए है क्योंकि अनियंत्रित अपवाद को रनटाइम पर जांचा जाता है।

इसके अलावा, अनियंत्रित अपवाद की अन्य कार्यक्षमता उपर्युक्त कार्यक्रम के समान है।

दिलचस्प लेख...