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

Java Exception Handling परिचय

एक exception वह है जो तब होता है जब program चलते समय कुछ गलत हो जाता है, चलते हुए एक rock पर trip करने जैसा। इसे handle करने का मतलब है गिरने के बजाय क्या करना है यह plan करना।
Syntax
java
try {
    // code that may throw
} catch (ExceptionType e) {
    // handle
}

What is an Exception?

एक exception एक object है जो Java बनाता और throw करता है जब कुछ program के normal flow को disrupt करता है, जैसे zero से divide करना या एक null reference access करना।

Program को सीधे crash होने देने के बजाय, Java आपको उस object को catch करने और respond करने का तरीका decide करने देता है।

उदाहरण: What is an Exception?

java
public class Main {
	public static void main(String[] args) {
		try {
			int result = 10 / 0; // throws ArithmeticException
		} catch (ArithmeticException e) {
			System.out.println("Caught: " + e.getMessage());
		}
	}
}

Checked vs Unchecked Exceptions

Checked exceptions (जैसे IOException) को compile time पर declared या caught होना चाहिए क्योंकि compiler आपको यह acknowledge करने पर मजबूर करता है कि failure possible है; unchecked exceptions (जैसे NullPointerException) RuntimeException extend करते हैं और बिना किसी ऐसी warning के surface हो सकते हैं।

उदाहरण: Checked vs Unchecked Exceptions

java
public class Main {
	public static void main(String[] args) {
		try {
			throw new RuntimeException("Unchecked"); // no need to declare it
		} catch (RuntimeException e) {
			System.out.println(e.getMessage());
		}
	}
}

Why Handle Exceptions?

Exceptions handle करना एक failure को program state चुपचाप corrupt करने या पूरी application crash होने से रोकता है। यह आपको user को एक raw stack trace के बजाय एक meaningful message देने देता है, या एक safe default पर fall back करके चलते रहने देता है।

उदाहरण: Why Handle Exceptions?

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("Handled gracefully instead of crashing");
		}
	}
}

Common Exception Types

आपको लगातार मिलने वाली कुछ exceptions में NullPointerException (null पर एक method call करना), ArrayIndexOutOfBoundsException (invalid array index), और ArithmeticException (जैसे एक int को zero से divide करना) शामिल हैं।

इन्हें नाम से पहचानना debugging काफी speed up करता है।

उदाहरण: Common 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 s and set it to null
			String s = null;
			s.length();
		// Handle NullPointerException if it was thrown
		} catch (NullPointerException e) {
			// Print a line to the console
			System.out.println("Caught a NullPointerException");
		}
	}
}

The try-catch-finally Concept

try block वह code रखता है जो fail हो सकता है, catch define करता है कि अगर यह fail हो तो कैसे respond करना है, और finally चाहे exception हुआ हो या नहीं चलता है — आमतौर पर open files या network connections जैसे resources release करने के लिए उपयोग होता है।

उदाहरण: The try-catch-finally Concept

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 result and set it to 10 / 0
			int result = 10 / 0;
		// Handle ArithmeticException if it was thrown
		} catch (ArithmeticException e) {
			// Print a line to the console
			System.out.println("Error occurred");
		// Always runs, whether or not an exception occurred
		} finally {
			// Print a line to the console
			System.out.println("Runs regardless");
		}
	}
}
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. Exception को बहुत broadly catch करना और असली bugs छुपाना।
  2. Checked exceptions नज़रअंदाज़ करना, जिन्हें compiler आपको handle या declare करने पर मजबूर करता है।
  3. एक empty catch block छोड़ना, इसलिए failures silent हैं।

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.