Exception Management

Tutorial 1 of 4

1. Introduction

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:

  • Understand what exceptions are and when they occur
  • Handle exceptions using try, catch, finally blocks
  • Throw your own exceptions

Prerequisites: Basic knowledge of JavaScript syntax and structures, HTML, and CSS.

2. Step-by-Step Guide

What are exceptions?

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.

Handling exceptions

JavaScript provides a mechanism to handle exceptions, using try, catch, and finally blocks.

  • The try block contains the code that might throw an exception.
  • The catch block contains the code that will execute if an exception is thrown in the try block.
  • The finally block contains the code that will run regardless of whether an exception is thrown or not.

Throwing exceptions

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.

3. Code Examples

Basic exception handling

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');
}

Throwing exceptions

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!
}

4. Summary

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.

5. Practice Exercises

  1. Write a JavaScript function that divides two numbers. The function should throw an exception if the divisor is zero.

  2. 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

  1. Division function
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!
  }
}
  1. Accessing object properties
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
}