JS Error ऑब्जेक्ट
In this page:
error.name
error.message
error.stack
name, message, और stack Properties
हर Error object तीन मुख्य properties रखता है: .name error के type की पहचान कराता है (जैसे TypeError), .message वह human-readable description रखता है जो आपने (या engine ने) दी, और .stack उन function calls का एक trace रखता है जो उस जगह तक पहुंचाए जहाँ error बनाया गया, यह ठीक-ठीक जानने के लिए बहुमूल्य है कि कुछ कहाँ गलत हुआ।
उदाहरण: The name, message, and stack Properties
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
null.name;
// Catch any error, bound to `e`
// Catch any error, bound to `e`
} catch (e) {
// Print `e.name, e.message` to the console
// Print `e.name, e.message` to the console
console.log(e.name, e.message);
// Print `e.stack` to the console
// Print `e.stack` to the console
console.log(e.stack);
}
Non-Error Values Throw करना
JavaScript का throw statement बिल्कुल कोई भी value स्वीकार करता है -- एक string, एक number, एक plain object -- सिर्फ एक Error instance ही नहीं, पर ऐसा करने से standard name/message/stack structure खो जाता है, जिससे thrown value को किसी ऐसे catch block में लगातार काम में लेना मुश्किल हो जाता है जो एक असली Error की उम्मीद करता है।
उदाहरण: Throwing Non-Error Values
try {
throw "just a string"; // not an Error instance
} catch (e) {
console.log(typeof e, e); // no .name/.message/.stack structure
}
Error में Custom Properties जोड़ना
standard तीन properties से आगे, आप किसी Error instance को बनाने के बाद उस पर सीधे अतिरिक्त custom properties जोड़ सकते हैं -- जैसे कोई खास error code, एक HTTP status, या जो fail हुआ उससे जुड़ा contextual data -- बिना पूरे custom subclass की ज़रूरत के base Error object को बढ़ाते हुए।
उदाहरण: Adding Custom Properties to an Error
// Declare the constant `err` as a new `Error` instance
// Declare the constant `err` as a new `Error` instance
const err = new Error("Something failed");
// Assign "E_CUSTOM" to `err.code`
// Assign "E_CUSTOM" to `err.code`
err.code = "E_CUSTOM";
// Assign `500` to `err.statusCode`
// Assign `500` to `err.statusCode`
err.statusCode = 500;
// Print `err.code, err.statusCode, err.message` to the console
// Print `err.code, err.statusCode, err.message` to the console
console.log(err.code, err.statusCode, err.message);
cause Property
एक हाल ही का addition, cause option (जो Error के constructor को दूसरे argument के रूप में पास किया जाता है: new Error(message, { cause: originalError })) आपको errors को एक साथ chain करने देता है, original underlying error को एक ज़्यादा specific, higher-level error में लपेटते हुए भी बचाए रखते हुए।
उदाहरण: The cause Property
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
// Throw a new `Error` with message "Original failure"
// Throw a new `Error` with message "Original failure"
throw new Error("Original failure");
// Catch any error, bound to `original`
// Catch any error, bound to `original`
} catch (original) {
// Throw a new `Error` with message `"Higher-level failure", { cause: original }`
// Throw a new `Error` with message `"Higher-level failure", { cause: original }`
throw new Error("Higher-level failure", { cause: original });
}
// Catch any error, bound to `e`
// Catch any error, bound to `e`
} catch (e) {
// Print `e.message, "caused by:", e.cause.message` to the console
// Print `e.message, "caused by:", e.cause.message` to the console
console.log(e.message, "caused by:", e.cause.message);
}
Logging के लिए Errors को Serialize करना
एक Error object डिफ़ॉल्ट रूप से JSON.stringify() से उपयोगी तरीके से serialize नहीं होता -- इसका enumerable-property scan name, message, और stack को छोड़ देता है, क्योंकि वे base Error prototype पर non-enumerable के रूप में define हैं -- इसलिए error details को JSON form में भेजने या log करने के लिए पहले manually ज़रूरी properties निकालनी पड़ती हैं।
उदाहरण: Serializing Errors for Logging
const err = new Error("Failed");
console.log(JSON.stringify(err)); // "{}" - name/message/stack are non-enumerable
console.log(JSON.stringify({ name: err.name, message: err.message }));
- यह मान लेना कि हर thrown value असल में एक Error object है -- JavaScript कोई भी value throw करने देता है (एक string, एक number), जिसमें Error के दिए जाने वाले standard name/message/stack properties नहीं होते।
- किसी error को log करते समय stack property को overwrite करना या नजरअंदाज करना, वह exact call-path information खोते हुए जो यह बताने में मदद करती कि error असल में कहाँ से शुरू हुई।
- बिना किसी descriptive message के एक नया Error() बनाना, एक ऐसा error produce करते हुए जो technically code के लिए informative है लेकिन बाद में log पढ़ने वाले किसी के लिए बेकार vague।
- new Error(message) एक Error object बनाता है जिसमें .name (डिफ़ॉल्ट रूप से "Error"), .message (आपका description), और .stack (एक call trace) होता है।
- Built-in error subtypes (TypeError, RangeError, और दूसरे) सभी base Error object से inherit करते हैं, उसकी core properties share करते हुए।
- JavaScript technically कोई भी value throw करने देता है, सिर्फ एक Error object नहीं -- लेकिन consistency के लिए एक असली Error (या उसकी subclass) throw करना strongly recommended है।
Error object और उसकी core properties को JavaScript के सबसे शुरुआती versions से हर browser में support मिला है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: