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

Java की Exception Hierarchy

Java के exceptions general problems को parents और specific वालों को children रखते हुए एक family tree में organized हैं। Tree जानना आपको सही तरह का catch करने में मदद करता है।
Syntax
java
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

java
// 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());
	}
}

Error vs Exception

Error आपके program के control से बाहर serious problems represent करता है, जैसे OutOfMemoryError, जिन्हें आपको आमतौर पर catch करने की कोशिश नहीं करनी चाहिए; Exception उन conditions को represent करता है जिनकी आपका code reasonably उम्मीद कर सकता है और recover कर सकता है।

उदाहरण: Error vs Exception

java
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
	}
}

RuntimeException Class

RuntimeException सभी unchecked exceptions का superclass है, जैसे NullPointerException और ArithmeticException — ये expected external failures के बजाय programming bugs signal करते हैं।

उदाहरण: RuntimeException Class

java
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());
		}
	}
}

Polymorphism in Exceptions

चूंकि exceptions एक class hierarchy बनाते हैं, एक supertype के लिए लिखा एक catch block (जैसे Exception) इसकी किसी भी subtypes को भी catch करेगा — इसलिए catch order मायने रखता है: ज़्यादा specific exception types को broader वालों से पहले listed होना चाहिए।

उदाहरण: Polymorphism in Exceptions

java
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());
		}
	}
}

Catching Multiple Exception Levels

आप एक single risky operation से hierarchy के अलग levels पर कई exceptions catch कर सकते हैं, आपको एक specific known failure precisely handle करने देते हुए जबकि अब भी किसी unexpected चीज़ के लिए एक broader catch-all रखते हुए।

उदाहरण: Catching Multiple Exception Levels

java
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());
		}
	}
}
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. Throwable या Error catch करना, जो OutOfMemoryError जैसी serious problems भी catch करता है।
  2. यह मान लेना कि RuntimeException को catch किया जाना चाहिए, जब यह unchecked है।
  3. Catch blocks को इस order में रखना कि parent पहले आए, जो एक 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.