Java के कई Exceptions
In this page:
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
// 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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Ordering Catch Blocks Correctly
जब कई catch blocks उपयोग किए जाते हैं, एक subclass exception type को हमेशा इसके superclass से पहले listed होना चाहिए, क्योंकि Java catch blocks को order में जांचता है और एक पहले वाला, broader catch नहीं तो बाद वाले, ज़्यादा specific एक को unreachable बना देगा।
उदाहरण: Ordering Catch Blocks Correctly
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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Catching a Common Superclass
Exception या RuntimeException जैसे एक general superclass catch करना एक block से इसके किसी भी subclasses handle करता है, जो convenient है लेकिन throw हुए exact exception के आधार पर अलग react करने की क्षमता खो देता है।
उदाहरण: Catching a Common Superclass
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());
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
- उसी multi-catch में एक subclass और इसके parent को list करना, जैसे
catch (IOException | Exception e), जो एक compile error है। - Multi-catch variable को assign करना, जो एक compile error है क्योंकि यह implicitly final है।
- एक specific से पहले एक general catch रखना, जो एक compile error है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: