In this tutorial, you will learn about the types of errors in JavaScript.
When you work in javascript programming language. If you write something wrong in script, So you must have seen that while working in it some errors come. There is some reason for this.
During you are writing a script, you get an error from writing some syntax wrong, or by writing some logic wrong. Or the executes the script and some error occurs.
JavaScript also has some similar errors. And each error comes for a different reason.
JavaScript error types
JavaScript has six types of errors that may raises during the script execution:
-
EvalError
-
RangeError
-
ReferenceError
-
SyntaxError
-
TypeError
-
URIError
EvalError
JavaScript raise the EvalError
when you use eval()
function in the wrong manner. See the following example:
let e = new eval();
Error:
TypeError: eval is not a constructor
However, web browsers often throw the TypeError
instead of EvalError
in this situation.
RangeError
The RangeError
happens when a number is not in its range. See the following example:
try {
let list = Array(Number.MAX_VALUE);
} catch (error) {
console.log(error.name); // "RangeError"
console.log(error.message); // "Invalid array length"
}
Error
RangeError
Invalid array length
Inside the try
block causes a RangeError
because array length not defined here.
ReferenceError
The ReferenceError
raises when you reference a variable, a function, or an object that does not exist. See the following example:
try {
var a = a + c;
} catch (error) {
console.log(error.name); // "ReferenceError"
console.log(error.message); // "c is not defined"
}
Error:
ReferenceError
c is not defined
In this example, the variable c
does not exist therefore it causes a ReferenceError
.
SyntaxError
The SyntaxError
raises in a string that you pass to the eval()
function. See the following example:
try {
eval('a x b');
} catch (error) {
console.log(error.name); // "SyntaxError"
console.log(error.message); // "Unexpected identifier"
}
Error
SyntaxError
Unexpected identifier
Outside the eval()
function, JavaScript stops executing the code whenever it finds a SyntaxError
.
TypeError
The TypeError
happens if a variable is of an unexpected type or access to a nonexistent method.
try {
let x = new "String";
} catch(error) {
console.log(error.name); // "TypeError"
console.log(error.message); // "String" is not a constructor"
}
Error:
TypeError
"String" is not a constructor
In this example, we tried to create a new instance of a literal string which caused a TypeError error.
let db = {
host: 'localhost',
port: 3306,
user: 'root',
password: 'secret'
};
try {
db.connect();
} catch (error) {
console.log(error.name); // "TypeError"
console.log(error.message); // "db.connect is not a function"
}
In this example, we tried to access the connect()
method of the db
object, which does not exist, therefore a TypeError
error occurred.
URIError
The URIError
error raises when using the encodeURI()
or decodeURI()
with a malformed URI. See the following example:
console.log(encodeURI('\uDFFF'));
Throwing errors
To throw an error, you can use the throw
js operator. See the following example:
throw 'ABC';
throw 123;
Whenever the JavaScript reaches the throw operator, it halts the execution of the script immediately. In that case, to continue the script execution, you need to use the try...catch statement to catch the value that was thrown. See the following example:
try {
throw 123;
} catch (error) {
console.log(error); // 123
}
console.log('continue!'); // "continue!"
You can also use the typeError mentioned earlier as an error to throw. See the following example:
function add(a, b) {
if (typeof a !== 'number') {
throw TypeError('The first argument must be a number');
}
if (typeof b !== 'number') {
throw TypeError('The second argument must be a number');
}
return a + b;
}
add('string', 1);
Error
TypeError: The first argument must be a number
Custom Error
You can create a custom error that obtains from a javascript built-in error. See the following:
function InvalidCallError(message) {
this.name = 'InvalidCallError';
this.message = message;
}
InvalidCallError.prototype = Object.create(Error.prototype);
InvalidCallError.prototype.constructor = Error;
Then, you can throw the custom error as shown in this example:
throw new InvalidCallError('Invalid function call');
Error
"Uncaught InvalidCallError: Invalid function call"