CodeGym /Java Blog /अनियमित /जावा कई अपवादों को पकड़ता है
John Squirrels
स्तर 41
San Francisco

जावा कई अपवादों को पकड़ता है

अनियमित ग्रुप में प्रकाशित
एकाधिक अपवादों को पकड़ने के बारे में सीखने से पहले, आपको जावा में मूल अपवाद हैंडलिंग से परिचित होना चाहिए। आगे बढ़ते हुए, हम मानते हैं कि आप जावा में ट्राई एंड कैच ब्लॉक से परिचित हैं।

जावा में हमें एकाधिक कैच ब्लॉक की आवश्यकता क्यों है?

जावा में विभिन्न प्रकार के अपवादों को संभालने के लिए एकाधिक कैच ब्लॉक का उपयोग किया जाता है। जावा 7 के लॉन्च से पहले, हमें एक विशिष्ट अपवाद को पकड़ने के लिए एक विशिष्ट कैच ब्लॉक की आवश्यकता थी। इसने निरर्थक कोड के ब्लॉक बनाए और इसलिए एक अक्षम दृष्टिकोण का परिणाम हुआ। पकड़े गए अपवादों को देखने के लिए निम्न उदाहरण देखें। यह विभिन्न प्रकार के अपवादों के लिए अलग-अलग कैच ब्लॉक का उपयोग करता है।

अलग कैच ब्लॉक का उपयोग करने का उदाहरण


import java.util.Arrays;

public class ExceptionHandler {

	public static void main(String[] args) {

		Integer[] colorsOfASpectrum = { 7, 6, 5, 4, 3, 2, 1, 0 };

		try {

			System.out.println("Total number of options on a dice are: " + Arrays.toString(colorsOfASpectrum));
		
	// 		un-comment the following line to see "Index Out of Bounds Exception"		
	//		colorsOfASpectrum[10] = 7; // Index Out of Bounds Exception
			
			System.out.println(colorsOfASpectrum[0] / 0);	// Arithmetic Exception

		} catch (ArrayIndexOutOfBoundsException e) {
			// This catch block executes in case of "Index Out of Bounds Exception"
			System.out.println("Array Index Out Of Bounds Exception " + e);
		
		} catch (ArithmeticException e) {
			// This catch block executes in case of "Arithmetic Exception"
			System.out.println("Arithmetic Exception " + e);
		}
		
		System.out.println("\n----Rest of the code executes here----");
	}
}

उत्पादन

एक स्पेक्ट्रम पर कुल रंग हैं: [7, 6, 5, 4, 3, 2, 1] अपवाद का सामना किया java.lang.ArithmeticException: / शून्य से ---- बाकी कोड यहां निष्पादित होता है ----
जैसा कि आप देख सकते हैं, ऊपर दिए गए उदाहरण में एक अपवाद को फेंके जाने पर एक अलग ब्लॉक को निष्पादित किया जाता है। विभिन्न प्रकार के अपवादों को पकड़ने के लिए कोड के एक ही ब्लॉक का उपयोग करके कई अपवादों को पकड़ने का एक अधिक कुशल तरीका है। निम्नलिखित उदाहरण पर एक नज़र डालें।

जावा में मल्टीपल कैच ब्लॉक का उपयोग करने का उदाहरण


import java.util.Arrays;

public class MultiExceptionHandler {

	public static void main(String[] args) {

		Integer[] colorsOfASpectrum = { 7, 6, 5, 4, 3, 2, 1 };

		try {

			System.out.println("Total colors on a spectrum are: " + Arrays.toString(colorsOfASpectrum));

	//		colorsOfASpectrum[10] = 7; // Index Out of Bounds Exception
			System.out.println(colorsOfASpectrum[0] / 0); // Arithmetic Exception

		} catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {
			// We don't need two different catch blocks for different kinds of exceptions
			// Both exceptions will be handled using this multiple catch block

			System.out.println("Exception Encountered " + e);
		}
		System.out.println("\n----Rest of the code executes here----");
	}
}

उत्पादन

आप अन्य प्रकार के अपवाद को प्रिंट करने के लिए लाइन 13 पर टिप्पणी नहीं कर सकते।
एक स्पेक्ट्रम पर कुल रंग हैं: [7, 6, 5, 4, 3, 2, 1] अपवाद का सामना किया java.lang.ArithmeticException: / शून्य से ---- बाकी कोड यहां निष्पादित होता है ----

निष्कर्ष

यह जावा कैच मल्टीपल एक्सेप्शन ब्लॉक का उपयोग करने का एक त्वरित अवलोकन था। एक चुनौती के रूप में, माता-पिता और बच्चे के अपवाद को एक ही ब्लॉक में प्रिंट करने का प्रयास करें। आपको बढ़ने के लिए सीखने और अभ्यास करने के लिए प्रोत्साहित किया जाता है। चीयर्स और खुश सीखना!
टिप्पणियां
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION