CodeGym /จาวาบล็อก /สุ่ม /Java จับข้อยกเว้นหลายรายการ
John Squirrels
ระดับ
San Francisco

Java จับข้อยกเว้นหลายรายการ

เผยแพร่ในกลุ่ม
ก่อนที่จะเรียนรู้เกี่ยวกับการตรวจจับข้อยกเว้นหลายรายการ คุณต้องทำความคุ้นเคยกับการจัดการข้อยกเว้น พื้นฐาน ใน Java จากนี้ไป เราถือว่าคุณคุ้นเคยกับ try and catch block ใน Java แล้ว

เหตุใดเราจึงต้องใช้ catch catch หลายอันใน Java

บล็อกจับจำนวนมากใน Java ใช้เพื่อจัดการข้อยกเว้นประเภทต่างๆ ก่อนเปิดตัว Java 7 เราจำเป็นต้องมี catch block เฉพาะเพื่อตรวจจับข้อยกเว้นเฉพาะ สิ่งนี้สร้างบล็อกของโค้ดที่ซ้ำซ้อนและส่งผลให้เกิดแนวทางที่ไม่มีประสิทธิภาพ ดูตัวอย่างต่อไปนี้เพื่อดูข้อยกเว้นที่พบได้ มันใช้บล็อก catch แยกต่างหากสำหรับข้อยกเว้นประเภทต่างๆ

ตัวอย่างการใช้ 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: / โดยศูนย์ ---- รหัสที่เหลือจะดำเนินการที่นี่----
อย่างที่คุณเห็น ในตัวอย่างข้างต้น บล็อกอื่นจะถูกดำเนินการเมื่อมีการโยนข้อยกเว้น มีวิธีที่มีประสิทธิภาพมากกว่าในการตรวจจับข้อยกเว้นหลายรายการโดยใช้บล็อกรหัสเดียวกันสำหรับการตรวจจับข้อยกเว้นประเภทต่างๆ ดูตัวอย่างต่อไปนี้

ตัวอย่างการใช้ 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: / โดยศูนย์ ---- รหัสที่เหลือจะดำเนินการที่นี่----

บทสรุป

นี่เป็นภาพรวมโดยย่อของการใช้ java catch บล็อกข้อยกเว้นหลายรายการ ลองพิมพ์พาเรนต์และข้อยกเว้นลูกในบล็อกเดียวกันตามความท้าทาย คุณควรที่จะเรียนรู้และฝึกฝนเพื่อเติบโต ไชโยและมีความสุขในการเรียนรู้!
ความคิดเห็น
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION