Shell Scripting / Advanced Shell Scripting Techniques

Error Management

This tutorial will introduce you to error handling in HTML. You'll learn how to catch and handle errors to prevent your program from crashing or behaving unpredictably.

Tutorial 4 of 4 4 resources in this section

Section overview

4 resources

Focuses on advanced concepts and best practices in shell scripting.

Tutorial: Error Management in HTML

1. Introduction

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:

  • What is Error Management
  • How to handle errors
  • Best practices for error management

Prerequisites:
This tutorial assumes that you have a basic understanding of HTML and JavaScript.

2. Step-by-Step Guide

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.

Try and Catch

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

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

Finally

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
}

3. Code Examples

Example 1: Catching and handling an error

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.

Example 2: Throwing a custom error

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.

4. Summary

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.

5. Practice Exercises

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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Time Zone Converter

Convert time between different time zones.

Use tool

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help