Custom Exceptions
Custom exceptions क्यों
Java की built-in exceptions सामान्य errors cover करती हैं, पर कभी आप ऐसी error चाहते हैं जो आपके program के नियम बताए — जैसे जितने पैसे हैं उससे ज़्यादा निकालना। Custom exception आपकी अपनी exception class है साफ़ नाम और message के साथ, errors समझना और संभालना आसान बनाकर।
Exception की झलक
try {
int x = 5 / 0; // ArithmeticException फेंकता है
} catch (ArithmeticException e) {
System.out.println("Caught: " + e.getMessage());
}
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());
}
इसे उठाने को 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 (
Exceptionextend) — catch याthrowsसे declare करना ज़रूरी। recoverable स्थितियों को इस्तेमाल कीजिए। - Unchecked (
RuntimeExceptionextend) — declare करना ज़रूरी नहीं। programming errors को इस्तेमाल कीजिए।
Checked तब चुनिए जब caller को case संभालने पर मजबूर होना चाहिए, unchecked उन bugs को जो बस fail हों।
आम गलतियाँ
- super को message न भेजना। तब
getMessage()null लौटाता है। - Checked exceptions को
throwsभूलना। Compiler को यह या catch चाहिए। - throw और throws में confusion।
throwइसे उठाता है;throwsdeclare करता है। - Custom exceptions का ज़्यादा इस्तेमाल। इन्हें सिर्फ़ अर्थपूर्ण, specific स्थितियों को बनाइए।
सारांश
- Custom exception आपके program की अपनी errors साफ़ बताती है।
- इसे
ExceptionयाRuntimeExceptionextend करके बनाइए। superको message भेजिए ताकिgetMessage()चले।- इसे उठाने को
throwऔर declare करने कोthrowsइस्तेमाल कीजिए। - Checked संभालनी ज़रूरी; unchecked ज़रूरी नहीं।
आगे, cleanup सीखिए final, finally और finalize से, या code व्यवस्थित कीजिए Java methods से।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
Java में custom exception क्या है?
InsufficientBalanceException। आप इसे Exception (या RuntimeException) extend करके बनाते हैं, जो error messages ज़्यादा साफ़ और अर्थपूर्ण बनाता है।Custom exception कैसे बनाऊँ?
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 में क्या अंतर है?
throws से declare या catch करना ज़रूरी है — compiler इसे लागू करता है। Unchecked exceptions (RuntimeException extend करती हैं) को यह ज़रूरी नहीं और आमतौर पर programming bugs दर्शाती हैं।