CodeGym /Java-blogg /Tilfeldig /Java Catch flere unntak
John Squirrels
Nivå
San Francisco

Java Catch flere unntak

Publisert i gruppen
Før du lærer om å fange opp flere unntak, må du være kjent med grunnleggende unntakshåndtering i Java. Fremover antar vi at du er kjent med en prøv-og-fang-blokk i Java.

Hvorfor trenger vi flere catch-blokker i Java?

Flere catch-blokker i Java brukes til å håndtere forskjellige typer unntak. Før Java 7 ble lansert, trengte vi en spesifikk catch-blokk for å fange opp et spesifikt unntak. Dette skapte blokker med redundant kode og resulterte derfor i en ineffektiv tilnærming. Ta en titt på følgende eksempel for å være vitne til fangede unntak. Den bruker separate fangstblokker for forskjellige typer unntak.

Eksempel ved bruk av separate fangstblokker


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----");
	}
}

Produksjon

Totalt antall farger på et spektrum er: [7, 6, 5, 4, 3, 2, 1] Unntak Encountered java.lang.ArithmeticException: / med null ----Resten av koden kjøres her----
Som du kan se, i eksemplet ovenfor blir en annen blokk utført når et unntak blir kastet. Det er en mer effektiv måte å fange opp flere unntak ved å bruke samme kodeblokk for å fange opp unntak av forskjellige typer. Ta en titt på følgende eksempel.

Eksempel ved bruk av Multiple Catch Block i 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----");
	}
}

Produksjon

Du kan fjerne kommentaren til linje 13 for utskrift med unntak av annen type.
Totalt antall farger på et spektrum er: [7, 6, 5, 4, 3, 2, 1] Unntak Encountered java.lang.ArithmeticException: / med null ----Resten av koden kjøres her----

Konklusjon

Dette var en rask oversikt over bruk av java catch multiple exceptions-blokk. Som en utfordring kan du prøve å skrive ut overordnet og underordnet unntak i samme blokk. Du oppmuntres til å lære og øve for å vokse. Skål og god læring!
Kommentarer
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION