Difference between throw new Error and throw someObject

In this tutorial, you will learn what is the difference between throw new Error and throw someObject in JavaScript.

Difference between throw new Error and throw someObject

The distinction between ‘throw new Error’ and ‘throw someObject’ in JavaScript lies in the way errors are handled. When you use ‘throw new Error,’ the error is encapsulated in the following structure −

{ name: 'Error', message: 'String you pass in the constructor' }

On the other hand, ‘throw someObject’ will throw the object as it is and halt any subsequent code execution within the try block, similar to ‘throw new Error.’

A comprehensive explanation of the Error object and the process of throwing custom errors is provided below.

The Error Object

What information can we extract from the Error object during an error event? The Error object, supported by all browsers, encompasses the following two properties:

  • name: The error’s name, specifically the name of the constructor function to which the error belongs.
  • message: A description of the error, with the specific details varying depending on the browser.

The ‘name’ property can yield six possible values, each corresponding to the names of the error’s constructors:

Error NameDescription
EvalErrorAn error in the eval() function has occurred.
RangeErrorAn out-of-range number value has occurred.
ReferenceErrorAn illegal reference has occurred.
SyntaxErrorA syntax error within the eval() function has occurred.
TypeErrorAn error in the expected variable type has occurred.
URIErrorAn error when encoding or decoding the URI has occurred.
JavaScript Error Types Demystified: A Quick Reference Guide.

For actual syntax errors not caught by try/catch/finally, the default browser error message associated with the error will be triggered. To catch such errors, you may utilize the onerror event.

Throwing Your Own Errors (Exceptions)

Rather than waiting for one of the six predefined error types to occur, causing control to shift automatically from the try block to the catch block, you have the option to explicitly throw your own exceptions. This approach is advantageous for defining your own error scenarios and determining when control should transition to the catch block.

Throwing Custom Errors in JavaScript: Unraveling the Impact of ‘throw’ and ‘throw new Error

When developers employ the ‘throw’ statement, as in the example ‘throw “I’m Evil”,’ the subsequent code execution is abruptly terminated, and the associated message string is exposed upon catching the error. This is demonstrated in the following snippet:

try {
  throw "I'm Evil";
  console.log("You'll never reach me", 123465);
} catch (e) {
  console.log(e); // I'm Evil
}

However, it’s important to note that any code following the ‘throw’ statement will be unreachable due to the termination of execution.

On the other hand, the ‘throw new Error’ statement, exemplified by ‘throw new Error(“I’m Evil”),’ not only terminates further execution but also exposes an error event with two parameters: ‘name’ and ‘message.’ This is illustrated in the snippet below:

try {
  throw new Error("I'm Evil");
  console.log("You'll never reach me", 123465);
} catch (e) {
  console.log(e.name, e.message); // Error: I'm Evil
}

For the sake of completeness, it’s worth mentioning that ‘throw Error(“I’m Evil”)’ is also syntactically valid, though it deviates from the conventional usage. The following snippet showcases this approach:

try {
  throw Error("I'm Evil");
  console.log("You'll never reach me", 123465);
} catch (e) {
  console.log(e.name, e.message); // Error: I'm Evil
}

Additionally, exploring the types associated with the Error object reveals that ‘typeof(new Error(“hello”))’ yields ‘object,’ while ‘typeof(Error)’ returns ‘function.'”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top