Open In App

Error Monitoring and Logging Techniques in JavaScript

Last Updated : 18 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Error monitoring and logging techniques in JavaScript are methods used to track, identify, and handle errors that occur within JavaScript code. When a program runs into an error, whether it’s a syntax mistake, a runtime issue, or a logical error, error monitoring tools help to capture and record information about the error.

The below list contains some of the error monitoring and logging techniques available in JavaScript:

Using try…catch block

This technique allows you to handle exceptions (errors) within a block of code. The try block contains the code that might throw an error, and the catch block is where we handle the error if one occurs.

Syntax:

try {
// Code that may throw an error
} catch (error) {
// Code to handle the error
}

Example: The below code practically implements the try/catch block in JavaScript to handle errors.

JavaScript
function divide(a, b) {
    try {
        if (b === 0) {
            throw new Error(
                'Division by zero is not allowed.');
        }
        return a / b;
    } catch (error) {
        console.error(
            'An error occurred:', error.message);
        return a / b;
    }
}

console.log(divide(10, 2));
console.log(divide(10, 0));
console.log(divide(8, 4));

Output:

5
An error occurred: Division by zero is not allowed.
Infinity
2

Using the console outputs

This is a simple logging technique to output messages to the browser console. It’s commonly used for debugging purposes.

Syntax:

console.log("Your Message Here");

Example: The below code implements console to monitor errors in JavaScript.

JavaScript
function checkNumber(num) {
    if (typeof num !== 'number') {
        console.error('Error: Input is not a number.');
        console.log('The passed number ' + num +
            ' is invalid. Because its type is: ' +
            typeof num);
    }
    else {
        console.log('The passed number ' +
            num + ' is valid.');
    }
}
checkNumber(10);
checkNumber('20');

Output:

The passed number 10 is valid.
Error: Input is not a number.
The passed number 20 is invalid. Because its type is: string

Using Custom Error Logging Functions

We can create custom functions to handle error logging according to the specific requirements. You can use the logging function to print the error.

Syntax:

function logError(error) {
// Custom error logging logic
console.error(error);
}

Example: The below code will explain how to create custom error logging functions in JavaScript.

JavaScript
function logError(errorMsg) {
    console.warn("Custom Error:", errorMsg);
}

function product(a, b) {
    try {
        if (b === 0) {
            throw new Error(
                "Multiplication with zero with always be zero");
        }
        return a * b;
    } catch (error) {
        logError(error.message);
        return a * b;
    }
}
console.log(product(10, 2));
console.log(product(10, 0));
console.log(product(8, 4));

Output:

20
Custom Error: Multiplication with zero with always be zero
0
32


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads