Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🔴 Advanced  ·  Lesson 23

Exception Handling

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

What is Exception Handling?

An exception is an unexpected event that occurs during program execution and disrupts the normal flow — for example dividing by zero, accessing an array out of bounds, or opening a missing file. Exception handling lets your program deal with such errors gracefully instead of crashing.

The Five Keywords

KeywordUse
tryWraps the risky code.
catchHandles the exception if one occurs.
finallyAlways runs (cleanup), whether or not an exception occurred.
throwManually throws an exception.
throwsDeclares that a method might throw an exception.

Example

Java
public class Main {
    public static void main(String[] a) {
        try {
            int x = 10 / 0;        // risky
        } catch (ArithmeticException e) {
            System.out.println("Cannot divide by zero");
        } finally {
            System.out.println("Done");   // always runs
        }
    }
}
▶ Output
Cannot divide by zero
Done

Checked vs Unchecked Exceptions

TypeChecked atExamples
CheckedCompile time (must handle)IOException, SQLException
UncheckedRuntimeArithmeticException, NullPointerException, ArrayIndexOutOfBounds
💡 Why handle exceptions?

Without handling, one error crashes the whole program. With handling, you show a friendly message and keep running — essential for real apps.

⚠️ Common mistake

Never leave an empty catch {} block — it hides bugs. At least log the error.

Summary

  • Exceptions disrupt normal flow; handling prevents crashes.
  • Keywords: try, catch, finally, throw, throws.
  • finally always runs; checked exceptions must be handled, unchecked occur at runtime.

Exception Handling क्या है?

Exception program चलते समय होने वाली एक अनचाही घटना है जो सामान्य flow को रोक देती है — जैसे zero से भाग देना, array की सीमा से बाहर जाना, या गायब file खोलना। Exception handling ऐसी errors को crash हुए बिना संभालने देता है।

पाँच Keywords

Keywordउपयोग
tryजोखिम भरे code को लपेटता है।
catchexception होने पर संभालता है।
finallyहमेशा चलता है (cleanup)।
throwmanually exception फेंकता है।
throwsबताता है कि method exception फेंक सकता है।

उदाहरण

Java
try {
    int x = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero");
} finally {
    System.out.println("Done");
}
▶ Output
Cannot divide by zero
Done

Checked vs Unchecked

प्रकारकब checkउदाहरण
CheckedCompile time (संभालना ज़रूरी)IOException, SQLException
UncheckedRuntimeArithmeticException, NullPointerException
💡 क्यों संभालें?

बिना handling के एक error पूरा program crash कर देती है। handling से friendly message दिखाकर program चलता रहता है।

⚠️ आम गलती

खाली catch {} block कभी न छोड़ें — यह bugs छुपाता है। कम से कम error log करें।

सारांश

  • Exceptions normal flow रोकती हैं; handling crash रोकती है।
  • Keywords: try, catch, finally, throw, throws।
  • finally हमेशा चलता है; checked ज़रूरी, unchecked runtime पर।
← Back to Java Tutorial
🔗

Share this topic with a friend

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

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

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

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.