🔴 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

  • try holds risky code; catch handles the error; finally always 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।

सारांश

  • try risky code रखता है; catch error handle करता है; finally हमेशा चलता है।
  • Runtime errors से crash रोकने के लिए use करें।
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n