JS Error Types
In this page:
throw new Error("message");
throw new TypeError("message");
throw new RangeError("message");
Error
Error is the base type all built-in JavaScript errors inherit from, and is also what you extend when defining your own custom error classes, since every specific error type ultimately shares its core behavior.
उदाहरण: 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 is thrown when a value isn't of the type an operation expects — for example, calling a method on undefined or trying to invoke something that isn't a 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 is thrown when code refers to a variable that doesn't exist in any accessible scope, such as using an identifier before it's declared or after a 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 is thrown when code can't even be parsed — invalid syntax, mismatched brackets, or malformed JSON passed to JSON.parse() are common triggers, and unlike other errors it usually can't be caught if it happens at load time.
उदाहरण: 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);
}
Other Built-in Error Types
Other built-ins include RangeError (a value outside its allowed range, like an invalid array length) and URIError (malformed input to encodeURI/decodeURI functions).
उदाहरण: Other Built-in Error Types
try {
new Array(-1); // invalid array length
} catch (e) {
console.log(e instanceof RangeError, e.message);
}
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: