← Back to JavaScript Course | Chapter 8: Error Handling | Lesson 6 of 6

JS Error ऑब्जेक्ट

हर JavaScript error, चाहे built-in हो या custom, आखिरकार properties के एक consistent सेट वाला एक object है -- name (error का type), message (एक description), और stack (यह कहाँ हुआ इसका एक trace) -- और इस base Error object को समझना ही वह चीज़ है जो सभी built-in error types, और custom ones को, काम करने के लिए consistent और predictable बनाती है।
Syntax
javascript
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 बनाया गया, यह ठीक-ठीक जानने के लिए बहुमूल्य है कि कुछ कहाँ गलत हुआ।

Note: debugging के दौरान err.stack को log करें (सिर्फ err.message नहीं) जब भी आपको ठीक-ठीक जानना हो कि code में error असल में कहाँ हुआ, न कि सिर्फ यह कि वह क्या कहता है।
Warning: .stack का exact format सभी JavaScript engines में standardized नहीं है -- यह इंसानों के debugging के लिए बेहद उपयोगी है, पर structured जानकारी निकालने के लिए इसे programmatically parse नहीं करना चाहिए।

उदाहरण: The name, message, and stack Properties

javascript
// 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 की उम्मीद करता है।

Note: हमेशा एक साधारण string या plain object के बजाय एक असली Error (या उसका कोई subclass) throw करें, ताकि आपके codebase का हर catch block एक जैसे consistent shape पर भरोसा कर सके।
Warning: ऐसा catching code जो मान लेता है कि err.message हमेशा मौजूद है, अगर thrown value एक असली Error की जगह एक साधारण string या plain object हो तो fail हो जाएगा (या undefined देगा)।

उदाहरण: Throwing Non-Error Values

javascript
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 को बढ़ाते हुए।

Note: जब पूरा custom Error subclass उस स्थिति के लिए ज़्यादा लगे, तो किसी error पर साफ नाम वाली कुछ थोड़ी सी custom properties (जैसे .code या .statusCode) जोड़ें।
Warning: किसी codebase के अलग-अलग हिस्सों में असंगत तरीके से कई ad-hoc custom properties जोड़ना errors को predictable तरीके से संभालना मुश्किल बना सकता है -- pattern आम होने पर एक proper custom Error class पर विचार करें।

उदाहरण: Adding Custom Properties to an Error

javascript
// 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 में लपेटते हुए भी बचाए रखते हुए।

Note: किसी low-level error को catch करके एक ज़्यादा descriptive, higher-level error re-throw करते समय cause option इस्तेमाल करें, ताकि इस प्रक्रिया में original underlying cause खो न जाए।
Warning: cause property अपेक्षाकृत हाल का addition है (ES2022) -- हालांकि सभी current major browsers में supported है, ध्यान रखें कि यह किसी बहुत पुराने browser environment में मौजूद न हो।

उदाहरण: The cause Property

javascript
// 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 निकालनी पड़ती हैं।

Note: Error instance को सीधे पास करने के बजाय, इसे JSON.stringify() को पास करने से पहले अपनी ज़रूरी खास error properties (name, message, और कोई भी custom properties) के साथ manually एक plain object बनाएं।
Warning: JSON.stringify(someError) आमतौर पर एक बेकार खाली object ({}) देता है, यह उन सभी के लिए एक आम हैरानी है जो उम्मीद करते हैं कि यह error की details अपने आप capture कर लेगा।

उदाहरण: Serializing Errors for Logging

javascript
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 }));
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि हर thrown value असल में एक Error object है -- JavaScript कोई भी value throw करने देता है (एक string, एक number), जिसमें Error के दिए जाने वाले standard name/message/stack properties नहीं होते।
  2. किसी error को log करते समय stack property को overwrite करना या नजरअंदाज करना, वह exact call-path information खोते हुए जो यह बताने में मदद करती कि error असल में कहाँ से शुरू हुई।
  3. बिना किसी 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.