Throw in JavaScript

In JavaScript, handling errors or exceptions that arise during program execution is referred to as exception handling. By using JavaScript's tools for handling exceptions gracefully, programmers can respond to failures in a controlled way and avoid program crashes.

The primary mechanism for exception handling in JavaScript is the try...catch statement. Here's how it works:

  • Try block: A try block contains the code that you wish to keep an eye out for exceptions.
  • Catch block: You may add a catch block that tells you what to do if an exception arises inside the try block right after the try block. You may specify how to handle the problem, such as recording it or giving the user an error message inside the catch block.
  • Finally, block (optional): Following the catch block, you may want to add a finally block. Whether or not there was an exception in the try block, the code inside the finally block will always run. This is helpful for clearing out things like resources.

Example:

The throw statement is used in JavaScript exception handling to create and "throw" an exception manually. It enables programmers to clearly state that a bug or other unusual situation has happened in their code. Upon executing a throw statement, the program halts and transfers control to the catch clause of the closest enclosing try to block or, in the event that no such block is present, to the global error handler.

Syntax:

In this case, expression stands for the value you wish to throw, which is usually an error object. Although any data type may be used for this expression, it is usually used to toss instances of the built-in Error object or any of its subclasses to offer more specific error information.

Demonstration of Throw in JavaScript

Here are a few examples demonstrating the usage of the throw statement in JavaScript exception handling:

  • Throwing a custom error message:

Code:

The validateAge function checks if an age parameter is negative, causing an error message. A try...catch block is used to catch exceptions.

1. Throwing a specific error type:

Code:

If y is not zero, the division method throws an error object. If this is the case, any exceptions are caught and logged to the console using a try...catch block.

2. Throwing an error with additional information:

Code:

When an input is found to be false, the validateInput method throws a ValidationError along with an invalid input and a configurable error message. It accepts a message and input, extends the built-in Error class, and logs an exception if it's a ValidationError.

3. Throwing a custom error object:

Code:

  • The built-in Error class is extended with the definition of the ValidationError class.
  • Its constructor uses super to set the values after receiving an input and a message.
  • When an input is found to be false, the validateInput method throws a ValidationError.
  • ValidateInput(null) is invoked inside the try...catch block, which raises an exception.
  • The ValidationError is caught by the catch block, which also records the error message and incorrect input to help identify it apart from other problems.

Exception handling in JavaScript is explained in detail in this article, with special attention to the `try...catch` and `throw` commands. It describes how to utilize these tools to handle mistakes gracefully so that programs react to problems and don't crash.

The `throw` statement enables the construction and tossing of bespoke error objects, while the `try...catch` statement serves as the main method for managing exceptions. Programmers can improve the resilience and dependability of their code by using the advice in this article.






Latest Courses