- Home
- javascript Tutorial
- exception-handling
Exception Handling
Exception handling is a process of handling unusual circumstances during runtime of a program. Sometimes in these circumstances, there is a need to execute an alternate set of instructions which changes the normal flow of the program.
In exception handling, there are four prominent statements
- The try statement
- The catch statement
- The throw statement
- The finally statement
JavaScript try and catch
- The try statement tests a block of code for errors during the execution of code.
- The catch statement also has a block of code which assumes control immediately after the try block throws an error.
The JavaScript statements try and catch come in pairs:
try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}
The throw Statement:
The throw statement allows us to create a customized error based on our requirement. This statement can return anything from a string to a boolean or an object.
throw 500; // throw a number
finally
The finally statement executes a block of code after the execution of the try/catch statement. This block of code executes regardless of what occurs during the execution of the try/catch statement.
Error names
Eval Error: Throws this error when there is an error in the eval statement.
Range Error: Throws this error when the number used is present outside the range of accepted legal values
Reference Error: Throws a reference error when there is a reference to an undeclared variable.
Syntax error: Throws a syntax error if the syntax of the code is not proper during runtime.
Type Error: Throws this error when the type of the variable is outside the range of accepted value types.
URI Error: Throws this error if we use illegal or invalid characters in the URI.
Eval Error
Throws this error when there is an error in the eval statement.
Range Error
Throws this error when the number used is present outside the range of accepted legal values
Example:
<!DOCTYPE html>
<html>
<body>
<p id="demo">
<script>
var num = 1;
try {
num.toPrecision(500);
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
Output:
RangeError
Reference Error
Throws a reference error when there is a reference to an undeclared variable.
Example:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var x;
try {
x = z + 1;
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
Output:
ReferenceError
Syntax Error
Throws a syntax error if the syntax of the code is not proper during runtime.
Example:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
try {
eval("alert('Hello)");
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
Output:
SyntaxError
Type Error
Throws this error when the type of the variable is outside the range of accepted value types.
Example:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var num = 1;
try {
num.toUpperCase();
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
Output:
TypeError
URI Error
Throws this error if we use illegal or invalid characters in the URI.
Example:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
try {
decodeURI("%");
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
Output:
URIError