Java Exception Handling परिचय
In this page:
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?
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());
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
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());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Why Handle Exceptions?
Exceptions handle करना एक failure को program state चुपचाप corrupt करने या पूरी application crash होने से रोकता है। यह आपको user को एक raw stack trace के बजाय एक meaningful message देने देता है, या एक safe default पर fall back करके चलते रहने देता है।
उदाहरण: Why Handle Exceptions?
// 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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Common Exception Types
आपको लगातार मिलने वाली कुछ exceptions में NullPointerException (null पर एक method call करना), ArrayIndexOutOfBoundsException (invalid array index), और ArithmeticException (जैसे एक int को zero से divide करना) शामिल हैं।
इन्हें नाम से पहचानना debugging काफी speed up करता है।
उदाहरण: Common 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 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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Exceptionको बहुत broadly catch करना और असली bugs छुपाना।- Checked exceptions नज़रअंदाज़ करना, जिन्हें compiler आपको handle या declare करने पर मजबूर करता है।
- एक empty
catchblock छोड़ना, इसलिए failures silent हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: