Different ways to handle JavaScript errors
When executing javascript code, different errors can occur. Errors can be coding errors made by the programmer, error due to wrong input, and other unforeseeable things
try and cath
The try statement allows you to define a block of code to be tested for errors while it is being executed.
The catch statement allows you to define a block of code to be executed if an error occurs in the try block
Example
<p id = "demo"></p>
<script>
try {
addAlert("hellow guest!");
}
catch(err){
document.getElementById("demo").innerHTML = err.message;
}
</script>
EvalError
The EvalError object indicates an error regarding the global eval() function. This exception is not thrown by JavaScript anymore, however, the EvalError object remains for compatibility.
EvalError()
Example
try {
throw new EvalError('Hello', 'someFile.js', 10);
} catch (e) {
console.log(e instanceof EvalError);
console.log(e.message);
console.log(e.name);
console.log(e.fileName);
console.log(e.lineNumber);
console.log(e.columnNumber);
console.log(e.stack);
}
InternalError
The InternalError indicates an error that occurred internally in the JavaScript engine.
InternalError()
Example
function loop(x) {
if (x >= 10) // "x >= 10" is the exit condition
return;
// do stuff
loop(x + 1); // the recursive call
}
loop(0);
RangeError
A Range is thrown if you use a number that is outside the range of legal values.
Example
var num = 1;
try {
num.toPercision(500);
}
catch(err){
document.getElementById("demo").innerHTML = err.name;
}
Type Error
A TypeError is thrown if you use a value that is outside the range of expected types:
Example
var num = 1;
try {
num.toUpperCase();
}
catch(err){
document.getElementById("demo").innerHTML = err.name;
}
URI(Uniform Resource Identifier) Error
A URIError is thrown if you use illegal characters in a URI function:
Example
try {
decodeURI("%%%");
}
catch(err){
document.getElementById("demo").innerHTML = err.name;
}
SyntaxError
The SyntaxError object represents an error when trying to interpret syntactically invalid code. It is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code.
SyntaxError()
Example
try {
eval('hoo bar');
} catch (e) {
console.error(e instanceof SyntaxError);
console.error(e.message);
console.error(e.name);
console.error(e.fileName);
console.error(e.lineNumber);
console.error(e.columnNumber);
console.error(e.stack);
}
ReferenceError
The ReferenceError object represents an error when a non-existent variable is referenced.
ReferenceError()
Example
try {
let a = undefinedVariable
} catch (e) {
console.log(e instanceof ReferenceError)
console.log(e.message)
console.log(e.name)
console.log(e.fileName)
console.log(e.lineNumber)
console.log(e.columnNumber)
console.log(e.stack)
}
switch
statement
the program executes the associated statement.
Example
switch (expression) {
case label_1:
statements_1
[break;]
case label_2:
statements_2
[break;]
…
default:
statements_def
[break;]
}
Utilizing Error objects
type of error, you may be able to use the name
and message
properties to get a more refined message
Example
function doSomethingErrorProne() {
if (ourCodeMakesAMistake()) {
throw (new Error('The message'));
} else {
doSomethingToGetAJavascriptError();
}
}
⋮
try {
doSomethingErrorProne();
} catch (e) { // NOW, we actually use `console.error()`
console.error(e.name); // logs 'Error'
console.error(e.message); // logs 'The message', or a JavaScript error message
}