In this tutorial, we will be looking at the concept of exception management, but not in HTML as it was originally requested. This is because HTML is a markup language and doesn't have exception handling capabilities. Instead, we'll explore exceptions in JavaScript, which is often used alongside HTML for web development.
JavaScript's exception handling mechanisms will help you understand how to manage errors effectively. This will improve the reliability and robustness of your code, leading to a better user experience.
By the end of this tutorial, you should be able to:
Prerequisites: Basic knowledge of JavaScript syntax and structures, HTML, and CSS.
Exceptions are unusual or exceptional conditions that can occur when your program runs. They usually disrupt the normal flow of your program, causing it to terminate prematurely. For example, attempting to reference a non-existent variable, or trying to use a function that hasn't been defined.
JavaScript provides a mechanism to handle exceptions, using try
, catch
, and finally
blocks.
try
block contains the code that might throw an exception.catch
block contains the code that will execute if an exception is thrown in the try
block.finally
block contains the code that will run regardless of whether an exception is thrown or not.You can throw your own exceptions using the throw
keyword. This allows you to define custom error conditions and handle them appropriately. You can throw anything from a string, number, Boolean or an object.
try {
// This code might throw an exception
let x = a; // a is not defined
} catch(err) {
// Handle the exception
console.log(err.message); // Output: a is not defined
} finally {
// This code runs regardless of an exception
console.log('This always runs');
}
try {
// This code might throw an exception
let value = -10;
// Throw an exception if value is negative
if(value < 0) {
throw 'Negative value error!';
}
} catch(err) {
// Handle the exception
console.log(err); // Output: Negative value error!
}
In this tutorial, you've learned about exceptions and how to manage them in JavaScript. You've seen how to use try
, catch
, and finally
blocks to handle exceptions and how to throw your own exceptions using the throw
keyword.
To continue learning about exception handling, you could explore different types of built-in Error objects in JavaScript and how to use them, or how to create your own custom Error objects.
Write a JavaScript function that divides two numbers. The function should throw an exception if the divisor is zero.
Write a JavaScript program that reads properties from an object. The program should throw and handle an exception if a non-existent property is accessed.
Solutions
function divide(a, b) {
try {
if(b == 0) {
throw 'Divide by zero error!';
}
return a / b;
} catch(err) {
console.log(err); // Output: Divide by zero error!
}
}
let obj = {name: 'John', age: 30};
try {
// This code might throw an exception
let value = obj.height; // height is not defined in object
console.log(value);
} catch(err) {
// Handle the exception
console.log(err.message); // Output: Cannot read property 'height' of undefined
}