Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📘 Lesson  ·  Lesson 32

Custom Exceptions

Custom exceptions क्यों

Java की built-in exceptions सामान्य errors cover करती हैं, पर कभी आप ऐसी error चाहते हैं जो आपके program के नियम बताए — जैसे जितने पैसे हैं उससे ज़्यादा निकालना। Custom exception आपकी अपनी exception class है साफ़ नाम और message के साथ, errors समझना और संभालना आसान बनाकर।

💡 Custom exception बस एक class है जो Exception (या RuntimeException) extend करती है।

Exception की झलक

try {
    int x = 5 / 0;             // ArithmeticException फेंकता है
} catch (ArithmeticException e) {
    System.out.println("Caught: " + e.getMessage());
}
Caught: / by zero

Exceptions वो errors हैं जिन्हें आप try-catch से catch कर सकते हैं। ArithmeticException जैसी built-in उपयोगी हैं, पर custom आपको अपनी error स्थितियाँ बताने देती हैं।

अपनी exception बनाना

class InsufficientBalanceException extends Exception {
    InsufficientBalanceException(String message) {
        super(message);        // Exception को message भेजो
    }
}

Custom exception बनाने को, Exception extend कीजिए और एक constructor जोड़िए जो super को message भेजे। अपना नाम वाला error type पाने को बस इतना ही चाहिए।

इसे throw और catch करना

void withdraw(int balance, int amount)
        throws InsufficientBalanceException {
    if (amount > balance) {
        throw new InsufficientBalanceException("Not enough money");
    }
    System.out.println("Withdrawn: " + amount);
}
// इस्तेमाल:
try {
    withdraw(100, 500);
} catch (InsufficientBalanceException e) {
    System.out.println("Error: " + e.getMessage());
}
Error: Not enough money

इसे उठाने को throw new ..., और declare करने को method में throws इस्तेमाल कीजिए। Caller इसे किसी भी exception की तरह catch करता है।

Message भेजना

super(message) को message भेजना मतलब catch होने पर getMessage() उसे लौटाता है। अच्छा message बताता है क्या गलत हुआ — "Not enough money" generic error से कहीं साफ़ है, जो आपको और अन्य developers को जल्दी debug करने में मदद करता है।

Checked बनाम unchecked

  • Checked (Exception extend) — catch या throws से declare करना ज़रूरी। recoverable स्थितियों को इस्तेमाल कीजिए।
  • Unchecked (RuntimeException extend) — declare करना ज़रूरी नहीं। programming errors को इस्तेमाल कीजिए।

Checked तब चुनिए जब caller को case संभालने पर मजबूर होना चाहिए, unchecked उन bugs को जो बस fail हों।

आम गलतियाँ

  • super को message न भेजना। तब getMessage() null लौटाता है।
  • Checked exceptions को throws भूलना। Compiler को यह या catch चाहिए।
  • throw और throws में confusion। throw इसे उठाता है; throws declare करता है।
  • Custom exceptions का ज़्यादा इस्तेमाल। इन्हें सिर्फ़ अर्थपूर्ण, specific स्थितियों को बनाइए।

सारांश

  • Custom exception आपके program की अपनी errors साफ़ बताती है।
  • इसे Exception या RuntimeException extend करके बनाइए।
  • super को message भेजिए ताकि getMessage() चले।
  • इसे उठाने को throw और declare करने को throws इस्तेमाल कीजिए।
  • Checked संभालनी ज़रूरी; unchecked ज़रूरी नहीं।

आगे, cleanup सीखिए final, finally और finalize से, या code व्यवस्थित कीजिए Java methods से।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Java में custom exception क्या है?
Custom exception आपकी अपनी exception class है जो आपके program-विशेष error बताती है, जैसे InsufficientBalanceException। आप इसे Exception (या RuntimeException) extend करके बनाते हैं, जो error messages ज़्यादा साफ़ और अर्थपूर्ण बनाता है।
Custom exception कैसे बनाऊँ?
ऐसी class बनाइए जो Exception (checked) या RuntimeException (unchecked) extend करे, और आमतौर पर एक constructor जोड़िए जो super को message भेजे। फिर आप इसे किसी भी exception की तरह throw कर सकते हैं।
throw और throws में क्या अंतर है?
throw असल में code के किसी बिंदु पर exception object फेंकता है। throws method के signature में लिखा जाता है यह declare करने को कि यह checked exception फेंक सकती है, callers को इसे संभालने की चेतावनी देकर।
Checked और unchecked exceptions में क्या अंतर है?
Checked exceptions (Exception extend करती हैं) को throws से declare या catch करना ज़रूरी है — compiler इसे लागू करता है। Unchecked exceptions (RuntimeException extend करती हैं) को यह ज़रूरी नहीं और आमतौर पर programming bugs दर्शाती हैं।
Generic के बजाय custom exceptions क्यों इस्तेमाल करूँ?
Custom exception किसी business rule को साफ़, specific नाम और message देती है, जैसे negative withdrawal। यह code पढ़ना आसान बनाता है और callers को उस exact स्थिति को अलग से catch व संभालने देता है।
← Back to Java Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicAccess Modifiers आपकी प्रगति: 32 / 61 (52%) · Lesson 33 / 61 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें