CodeGym /జావా బ్లాగ్ /యాదృచ్ఛికంగా /జావా క్యాచ్ బహుళ మినహాయింపులు
John Squirrels
స్థాయి
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