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

JS Error टाइप्स

JavaScript में अलग-अलग तरह के errors होते हैं, जैसे TypeError और ReferenceError, हर एक आपको एक अलग तरह की समस्या बताता है, जैसे dashboard पर अलग-अलग warning lights। इन्हें जानना चीज़ों को जल्दी ठीक करने में मदद करता है।
Syntax
javascript
throw new Error("message");
throw new TypeError("message");
throw new RangeError("message");

Error

Error वह base type है जिससे सभी built-in JavaScript errors inherit करते हैं, और यह वही है जिसे आप अपनी custom error classes define करते समय extend करते हैं, क्योंकि हर खास error type आखिरकार इसका core व्यवहार साझा करता है।

उदाहरण: Error

javascript
// 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 "Generic problem"
  // Throw a new `Error` with message "Generic problem"
  throw new Error("Generic problem");
// Catch any error, bound to `e`
// Catch any error, bound to `e`
} catch (e) {
  // Print `e instanceof Error, e.message` to the console
  // Print `e instanceof Error, e.message` to the console
  console.log(e instanceof Error, e.message);
}

TypeError

TypeError तब throw होता है जब कोई value उस type की नहीं होती जो operation को चाहिए — उदाहरण के लिए, undefined पर कोई method कॉल करना या किसी ऐसी चीज़ को invoke करने की कोशिश करना जो function ही नहीं है।

उदाहरण: TypeError

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 instanceof TypeError, e.message` to the console
  // Print `e instanceof TypeError, e.message` to the console
  console.log(e instanceof TypeError, e.message);
}

ReferenceError

ReferenceError तब throw होता है जब code किसी ऐसे variable को refer करता है जो किसी accessible scope में मौजूद ही नहीं है, जैसे किसी identifier को उसके declare होने से पहले या किसी typo के बाद इस्तेमाल करना।

उदाहरण: ReferenceError

javascript
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
  // Print `undeclaredVar` to the console
  // Print `undeclaredVar` to the console
  console.log(undeclaredVar);
// Catch any error, bound to `e`
// Catch any error, bound to `e`
} catch (e) {
  // Print `e instanceof ReferenceError, e.message` to the console
  // Print `e instanceof ReferenceError, e.message` to the console
  console.log(e instanceof ReferenceError, e.message);
}

SyntaxError

SyntaxError तब throw होता है जब code parse ही नहीं हो पाता — invalid syntax, mismatched brackets, या JSON.parse() को दिया गया malformed JSON इसके आम कारण हैं, और दूसरे errors के उलट अगर यह load time पर होता है तो इसे आमतौर पर catch नहीं किया जा सकता।

उदाहरण: SyntaxError

javascript
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
  // Call `JSON.parse("{bad json}")`
  // Call `JSON.parse("{bad json}")`
  JSON.parse("{bad json}");
// Catch any error, bound to `e`
// Catch any error, bound to `e`
} catch (e) {
  // Print `e instanceof SyntaxError, e.message` to the console
  // Print `e instanceof SyntaxError, e.message` to the console
  console.log(e instanceof SyntaxError, e.message);
}

अन्य Built-in Error Types

बाकी built-ins में RangeError (कोई value उसकी allowed range से बाहर, जैसे invalid array length) और URIError (encodeURI/decodeURI functions को दिया गया malformed input) शामिल हैं।

उदाहरण: Other Built-in Error Types

javascript
try {
  new Array(-1); // invalid array length
} catch (e) {
  console.log(e instanceof RangeError, e.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. ReferenceError (variable मौजूद नहीं है) को TypeError (variable मौजूद है लेकिन operation गलत है, जैसे undefined() कॉल करना) से confuse करना।
  2. किसी SyntaxError message को नजरअंदाज करना, जो किसी typo की ओर इशारा करता है जैसे missing bracket जो पूरी script रोक देता है।
  3. सभी errors को same तरीके से catch करना, जबकि error instanceof TypeError आपको हर type पर अलग तरीके से react करने देता है।
🔒

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.