🔴 Advanced · Lesson 23
Exception Handling
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 करें।