← Back to Core Java Course | Chapter 10: Exception Handling | Lesson 3 of 8

Java के कई Exceptions

एक single try block के बाद कई catch blocks आ सकते हैं, या एक block में कई exception types catch करने के लिए pipe operator उपयोग कर सकते हैं, एक program को अलग-अलग तरह की errors पर precisely respond करने देते हुए।
Syntax
java
try {
    // code
} catch (TypeA | TypeB e) {
    // handle either
} catch (TypeC e) {
    // handle C
}

Catching Multiple Exception Types

एक single try block के बाद कई catch blocks आ सकते हैं, हर एक एक अलग exception type handle करते हुए, एक program को इस आधार पर अलग respond करने देते हुए कि असल में कौन सी तरह की error हुई।

उदाहरण: Catching Multiple Exception Types

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Try code that might throw an exception
		try {
			// Declare arr and set it to new int[2]
			int[] arr = new int[2];
			// Print a line to the console
			System.out.println(arr[5]);
		// Handle ArrayIndexOutOfBoundsException if it was thrown
		} catch (ArrayIndexOutOfBoundsException e) {
			// Print a line to the console
			System.out.println("Array error");
		// Handle ArithmeticException if it was thrown
		} catch (ArithmeticException e) {
			System.out.println("Math error");
		}
	}
}

Multi-Catch with the Pipe Operator

Java का multi-catch syntax, एक catch block के parentheses के अंदर exception types के बीच pipe operator उपयोग करके, कई unrelated exception types को इसे duplicate किए बिना बिल्कुल वही handling code share करने देता है।

उदाहरण: Multi-Catch with the Pipe Operator

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Try code that might throw an exception
		try {
			// Declare arr and set it to new int[2]
			int[] arr = new int[2];
			// Print a line to the console
			System.out.println(arr[5] / 0);
		// Handle ArrayIndexOutOfBoundsException | ArithmeticException if it was thrown
		} catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {
			// Print a line to the console
			System.out.println("Caught: " + e.getClass().getSimpleName());
		}
	}
}

Ordering Catch Blocks Correctly

जब कई catch blocks उपयोग किए जाते हैं, एक subclass exception type को हमेशा इसके superclass से पहले listed होना चाहिए, क्योंकि Java catch blocks को order में जांचता है और एक पहले वाला, broader catch नहीं तो बाद वाले, ज़्यादा specific एक को unreachable बना देगा।

उदाहरण: Ordering Catch Blocks Correctly

java
public class Main {
	public static void main(String[] args) {
		try {
			int[] arr = new int[2];
			System.out.println(arr[5]);
		} catch (ArrayIndexOutOfBoundsException e) { // subclass listed first
			System.out.println("Specific catch");
		} catch (Exception e) { // superclass listed after
			System.out.println("General catch");
		}
	}
}

Catching a Common Superclass

Exception या RuntimeException जैसे एक general superclass catch करना एक block से इसके किसी भी subclasses handle करता है, जो convenient है लेकिन throw हुए exact exception के आधार पर अलग react करने की क्षमता खो देता है।

उदाहरण: Catching a Common Superclass

java
public class Main {
	public static void main(String[] args) {
		try {
			int result = 10 / 0;
		} catch (Exception e) { // catches any subclass of Exception
			System.out.println("Caught: " + e.getClass().getSimpleName());
		}
	}
}

When to Use Multi-Catch

Multi-catch सही tool है जब कई distinct exception types genuinely identical handling logic की हकदार हों, जबकि अलग catch blocks बेहतर हैं जब अलग exception types को meaningfully अलग responses चाहिए।

उदाहरण: When to Use Multi-Catch

java
public class Main {
	public static void main(String[] args) {
		try {
			Object obj = "text";
			Integer n = (Integer) obj; // ClassCastException
		} catch (ClassCastException | NullPointerException e) {
			System.out.println("Both handled identically");
		}
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. उसी multi-catch में एक subclass और इसके parent को list करना, जैसे catch (IOException | Exception e), जो एक compile error है।
  2. Multi-catch variable को assign करना, जो एक compile error है क्योंकि यह implicitly final है।
  3. एक specific से पहले एक general catch रखना, जो एक compile error है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.