CodeGym /בלוג Java /Random-HE /Java תפוס חריגים מרובים
John Squirrels
רָמָה
San Francisco

Java תפוס חריגים מרובים

פורסם בקבוצה
לפני שתלמדו על תפיסת חריגים מרובים, עליכם להכיר את הטיפול הבסיסי בחריגים ב-Java. בהמשך, אנו מניחים שאתה מכיר בלוק נסה ותפוס בג'אווה.

למה אנחנו צריכים מספר בלוקים של תפיסה ב-Java?

מספר בלוקים מסוג Catch ב-Java משמשים לטיפול בסוגים שונים של חריגים. לפני שהושק Java 7, היינו צריכים לחסום תפס ספציפי כדי לתפוס חריג ספציפי. זה יצר בלוקים של קוד מיותר ומכאן הביא לגישה לא יעילה. עיין בדוגמה הבאה כדי לראות חריגים שנתפסו. היא משתמשת בלוקי תפס נפרדים עבור סוגים שונים של חריגים.

דוגמה באמצעות Separate Catch Blocks

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: / ב-0 ---- שאר הקוד מופעלת כאן----
כפי שאתה יכול לראות, בדוגמה למעלה מבוצע בלוק אחר כאשר נזרק חריג. יש דרך יעילה יותר לתפוס חריגים מרובים באמצעות אותו בלוק קוד לתפיסת חריגים מסוגים שונים. תסתכל על הדוגמה הבאה.

דוגמה באמצעות Multiple Catch Block ב-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: / ב-0 ---- שאר הקוד מופעלת כאן----

סיכום

זו הייתה סקירה מהירה של השימוש ב-Java Catch Multiple Exception Block. כאתגר, נסה להדפיס את החריגה ההורה והילד באותו בלוק. מעודדים אותך ללמוד ולתרגל כדי לגדול. לחיים ולמידה מהנה!
הערות
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION