In this tutorial, we will learn about Error Management in HTML. The main goal is to understand how to catch and handle errors to prevent your program from crashing or behaving unpredictably.
By the end of this tutorial, you will learn:
Prerequisites:
This tutorial assumes that you have a basic understanding of HTML and JavaScript.
HTML itself does not have built-in error handling capabilities. However, JavaScript, which often works hand-in-hand with HTML in web development, does. We use JavaScript's try
, catch
, finally
, and throw
statements to handle runtime errors.
The try
statement allows you to define a block of code to test for errors while it's being executed. The catch
statement lets you define a block of code to be executed if an error occurs in the try block.
try {
// code to try or test
}
catch(err) {
// code to execute if an error occurs in the try block
}
throw
statement allows you to create custom errors. Technically you can throw an exception (throw an error). The exception can be a JavaScript String, a Number, a Boolean or an Object.
throw "Error2"; // String type Error
throw 42; // Number type Error
throw true; // Boolean type Error
throw {toString: function() { return "I'm an object!"; } };
The finally
statement lets you execute code after try
and catch
, regardless of the result.
try {
// code to try or test
}
catch(err) {
// code to handle errors
}
finally {
// code to be executed regardless of the try / catch result
}
try {
adddlert("Welcome guest!"); // this function is undefined, so an error will be thrown
}
catch(err) {
document.getElementById("demo").innerHTML = err.message; // display the error message
}
In this example, adddlert
is not a function or defined anywhere in the code, so it throws an error. The catch block catches the error and displays the error message.
let x = -1;
try {
if(x < 0) {
throw "Number is too low"; // throws an error if number is negative
}
}
catch(err) {
alert(err); // display the error message
}
In this example, if x
is negative, a custom error with the message "Number is too low" is thrown.
In this tutorial, we've learned about error handling in HTML with JavaScript. We've learned how to catch and handle errors to prevent your program from crashing or behaving unpredictably. Always remember that good error handling makes your web pages much more reliable and user friendly.
Exercise 1: Write a JavaScript program that tries to add a string to a number and catch the error.
Exercise 2: Write a JavaScript program that tries to call a function that does not exist and catch the error.
Exercise 3: Write a JavaScript program that throws a custom error if a number is even and catch the error.
Solutions:
// Exercise 1
try {
let result = 5 + "five"; // this will not throw an error but result in string "5five"
}
catch(err) {
console.log(err.message);
}
// Exercise 2
try {
nonExistentFunction(); // this function does not exist
}
catch(err) {
console.log(err.message);
}
// Exercise 3
let x = 2;
try {
if(x % 2 == 0) {
throw "Number is even";
}
}
catch(err) {
console.log(err);
}
In the first exercise, JavaScript does not throw an error when adding a string to a number. It simply converts the number to a string and concatenates them.
In the second exercise, the function nonExistentFunction
does not exist, so an error is thrown.
In the third exercise, a custom error is thrown if x
is an even number.