Java की Exception Hierarchy
In this page:
try {
// code
} catch (SpecificException e) {
// specific first
} catch (Exception e) {
// general last
}
The Throwable Class
Throwable Java की exception hierarchy के top पर बैठता है, इसके दो direct subclasses के रूप में Error और Exception के साथ — जो कुछ भी आप throw या catch कर सकते हैं वह आखिरकार इससे descend करता है।
उदाहरण: The Throwable Class
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Exception object and store it in t
Throwable t = new Exception("Something went wrong");
// Print a line to the console
System.out.println(t.getMessage());
}
}
Login to try C/C++/Java/PHP code in the editor
Error vs Exception
Error आपके program के control से बाहर serious problems represent करता है, जैसे OutOfMemoryError, जिन्हें आपको आमतौर पर catch करने की कोशिश नहीं करनी चाहिए; Exception उन conditions को represent करता है जिनकी आपका code reasonably उम्मीद कर सकता है और recover कर सकता है।
उदाहरण: Error vs Exception
public class Main {
public static void main(String[] args) {
try {
throw new Exception("Recoverable condition");
} catch (Exception e) {
System.out.println("Caught an Exception: " + e.getMessage());
}
// Error like OutOfMemoryError generally should not be caught
}
}
Login to try C/C++/Java/PHP code in the editor
RuntimeException Class
RuntimeException सभी unchecked exceptions का superclass है, जैसे NullPointerException और ArithmeticException — ये expected external failures के बजाय programming bugs signal करते हैं।
उदाहरण: RuntimeException Class
public class Main {
public static void main(String[] args) {
try {
int[] arr = new int[2];
System.out.println(arr[5]); // ArrayIndexOutOfBoundsException extends RuntimeException
} catch (RuntimeException e) {
System.out.println("Caught: " + e.getClass().getSimpleName());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Polymorphism in Exceptions
चूंकि exceptions एक class hierarchy बनाते हैं, एक supertype के लिए लिखा एक catch block (जैसे Exception) इसकी किसी भी subtypes को भी catch करेगा — इसलिए catch order मायने रखता है: ज़्यादा specific exception types को broader वालों से पहले listed होना चाहिए।
उदाहरण: Polymorphism in Exceptions
public class Main {
public static void main(String[] args) {
try {
throw new NullPointerException("npe");
} catch (Exception e) { // catches the subtype NullPointerException too
System.out.println("Caught via supertype: " + e.getMessage());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Catching Multiple Exception Levels
आप एक single risky operation से hierarchy के अलग levels पर कई exceptions catch कर सकते हैं, आपको एक specific known failure precisely handle करने देते हुए जबकि अब भी किसी unexpected चीज़ के लिए एक broader catch-all रखते हुए।
उदाहरण: Catching Multiple Exception Levels
public class Main {
public static void main(String[] args) {
try {
int[] arr = new int[2];
System.out.println(arr[5]);
} catch (ArrayIndexOutOfBoundsException e) { // specific level
System.out.println("Specific: " + e.getMessage());
} catch (RuntimeException e) { // broader level
System.out.println("General: " + e.getMessage());
}
}
}
Login to try C/C++/Java/PHP code in the editor
ThrowableयाErrorcatch करना, जोOutOfMemoryErrorजैसी serious problems भी catch करता है।- यह मान लेना कि
RuntimeExceptionको catch किया जाना चाहिए, जब यह unchecked है। - Catch blocks को इस order में रखना कि parent पहले आए, जो एक compile error है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: