CodeGym /Java Blog /यादृच्छिक /जावा कॅच एकाधिक अपवाद
John Squirrels
पातळी 41
San Francisco

जावा कॅच एकाधिक अपवाद

यादृच्छिक या ग्रुपमध्ये प्रकाशित केले
एकाधिक अपवाद पकडण्याबद्दल शिकण्यापूर्वी, तुम्हाला Java मधील मूलभूत अपवाद हाताळणीशी परिचित असणे आवश्यक आहे. पुढे जाणे, आम्ही असे गृहीत धरतो की तुम्ही Java मधील ट्राय अँड कॅच ब्लॉकशी परिचित आहात.

आम्हाला Java मध्ये एकाधिक कॅच ब्लॉक्सची आवश्यकता का आहे?

Java मधील एकाधिक कॅच ब्लॉक्स वेगवेगळ्या प्रकारचे अपवाद हाताळण्यासाठी वापरले जातात. Java 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: / by zero ----बाकीचा कोड येथे कार्यान्वित होतो----
तुम्ही बघू शकता, वरील उदाहरणात अपवाद टाकल्यावर वेगळा ब्लॉक अंमलात आणला जातो. विविध प्रकारचे अपवाद पकडण्यासाठी कोडचा समान ब्लॉक वापरून अनेक अपवाद पकडण्याचा अधिक कार्यक्षम मार्ग आहे. खालील उदाहरण पहा.

Java मध्ये एकाधिक कॅच ब्लॉक वापरण्याचे उदाहरण


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: / by zero ----बाकीचा कोड येथे कार्यान्वित होतो----

निष्कर्ष

जावा कॅच मल्टिपल एक्सेप्शन ब्लॉक वापरण्याचे हे एक द्रुत विहंगावलोकन होते. आव्हान म्हणून, त्याच ब्लॉकमध्ये पालक आणि मूल अपवाद मुद्रित करण्याचा प्रयत्न करा. तुम्हाला शिकण्यासाठी आणि वाढण्यासाठी सराव करण्यास प्रोत्साहित केले जाते. चिअर्स आणि आनंदी शिक्षण!
टिप्पण्या
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION