JavaScript Errors


JavaScript error handling is the process of detecting and responding to errors that occur during the execution of a JavaScript program. Runtime errors are those that happen when the script is running, thus the name. Professional software must be able to gracefully manage such runtime errors. Often, this entails providing the consumer with clearer and more detailed information about the issue.  Here are the basic steps for handling errors in JavaScript.


1. The try...catch Statement

An error handling JavaScript construct is the try...catch statement. The try block and the catch block make up both sections. The code that might raise an error is found in the try block. Control is sent to the catch block if an error occurs in this block. When an error is encountered in the try block, the catch block's function is run. It requires a representation of the error object as a parameter, which provides details about the problem that occurred.

When a try block error occurs, control is passed to the catch block, which runs the appropriate error-handling code. The catch block receives the error object as a parameter, which gives the code access to the error's details.

The try...catch statement is helpful for addressing unforeseen problems that may happen while a program is being executed. You can catch problems and save the application from crashing by enclosing potentially troublesome code in a try block.

Example

Preview

2. The try...catch...finally Statement

The try...catch...finally statement is a JavaScript construct used for error handling that includes a third block, the finally block. It is similar to the try...catch statement, but the finally block allows you to specify code that should be executed regardless of whether an error occurs. The try block contains the code that might throw an error. If an error occurs in this block, control is transferred to the catch block. If no error occurs, the catch block is skipped.

The catch block contains code that is executed when an error occurs in the try block. It takes an argument that represents the error object, which contains information about the error that occurred. The finally block contains code that is executed after the try and catch blocks, regardless of whether an error occurred. This block is useful for releasing resources, closing files, or cleaning up code after a block of code has executed.

The try block contains code that might throw an error. If an error occurs, control is transferred to the catch block, which will execute the code that handles the error. The finally block will then execute, regardless of whether an error occurred or not. The try...catch...finally statement is useful for handling errors and ensuring that code is executed regardless of whether an error occurred or not.

Example

Preview

3. Throwing Errors

In JavaScript, you can throw an error using the throw statement. This statement allows you to create custom error messages and control the flow of your code based on specific conditions. The throw statement takes an expression as its argument, which can be any value, including a string, number, or object. This expression is the error message that is displayed when the error is thrown.

Throwing errors is useful when you want to create custom error messages or to control the flow of your code based on specific conditions. By throwing errors, you can prevent unexpected behaviour and ensure that your code runs smoothly and efficiently.

Example

Preview

Error Types

The Error object is the base type of all errors and it has two main properties a name property that specifies the type of error, and a message property that holds a message describing the error in more detail. Any error thrown will be an instance of the Error object.

There are several different types of error that can occur during the execution of a JavaScript program, such as RangeError, ReferenceError, SyntaxError, TypeError, and URIError.

The following section describes each one of these error type in more detail:

RangeError

This error occurs when you try to use a value that is outside of the range of acceptable values. For example, if you try to create an array with a negative length.

Example

Preview

ReferenceError

This error occurs when you try to reference a variable or function that doesn't exist. For example, if you try to use a variable that hasn't been declared.

Example

Preview

SyntaxError

This error occurs when there is a problem with the syntax of your JavaScript code. For example, if you forget to add a semicolon at the end of a statement or use a reserved keyword as a variable name.

Example

Preview

TypeError

This error occurs when you try to perform an operation on a value that is of the wrong type. For example, if you try to add a string to a number.

Example

Preview

URIError

URIError is a type of error that occurs when a global function that expects a valid Uniform Resource Identifier (URI) is passed an invalid URI.

The specific error type can also be thrown manually using their respective constructor and the throw statement, e.g., to throw a TypeError you can use the TypeError() constructor.

Example

Preview