🔴 Advanced · Lesson 23
Exception Handling
What is an Exception?
An exception is an error that occurs while a program runs. Handling it with
try-catch prevents a crash.try-catch-finally
try {
int x = 10 / 0; // throws ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Always runs");
}Cannot divide by zero
Always runs
Common Exceptions
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, NumberFormatException.
Summary
tryholds risky code;catchhandles the error;finallyalways runs.- Use it to avoid crashes from runtime errors.
Exception क्या है?
Exception वह error है जो program चलते समय आती है।
try-catch से handle करने पर crash रुकता है।try-catch-finally
try {
int x = 10 / 0; // ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Always runs");
}Cannot divide by zero
Always runs
Common Exceptions
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, NumberFormatException।
सारांश
tryrisky code रखता है;catcherror handle करता है;finallyहमेशा चलता है।- Runtime errors से crash रोकने के लिए use करें।
💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.