JS Error टाइप्स
In this page:
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
// 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
// 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
// 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
// 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
try {
new Array(-1); // invalid array length
} catch (e) {
console.log(e instanceof RangeError, e.message);
}
ReferenceError(variable मौजूद नहीं है) कोTypeError(variable मौजूद है लेकिन operation गलत है, जैसेundefined()कॉल करना) से confuse करना।- किसी
SyntaxErrormessage को नजरअंदाज करना, जो किसी typo की ओर इशारा करता है जैसे missing bracket जो पूरी script रोक देता है। - सभी errors को same तरीके से catch करना, जबकि
error instanceof TypeErrorआपको हर type पर अलग तरीके से react करने देता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: