相关文章推荐
犯傻的小笼包  ·  遞增運算子(++) - ...·  1 月前    · 
跑龙套的青蛙  ·  HTTP headers - HTTP | MDN·  1 月前    · 
虚心的长颈鹿  ·  Object.freeze() - ...·  1 月前    · 
卖萌的水桶  ·  Vue ...·  2 年前    · 
热心的皮蛋  ·  Microsoft office ...·  2 年前    · 
function getRectArea(width, height) {
  if (isNaN(width) || isNaN(height)) {
    throw new Error("Parameter is not a number!");
try {
  getRectArea(3, "A");
} catch (e) {
  console.error(e);
  // Expected output: Error: Parameter is not a number!
    

Syntax

js
throw expression;
expression

The expression to throw.

Description

The throw statement is valid in all contexts where statements can be used. Its execution generates an exception that penetrates through the call stack. For more information on error bubbling and handling, see Control flow and error handling.

The throw keyword can be followed by any kind of expression, for example:

js
throw error; // Throws a previously defined value (e.g. within a catch block)
throw new Error("Required"); // Throws a new Error object

In practice, the exception you throw should always be an Error object or an instance of an Error subclass, such as RangeError. This is because code that catches the error may expect certain properties, such as message, to be present on the caught value. For example, web APIs typically throw DOMException instances, which inherit from Error.prototype.

Automatic semicolon insertion

The syntax forbids line terminators between the throw keyword and the expression to be thrown.

js
throw
new Error();

The code above is transformed by automatic semicolon insertion (ASI) into:

js
throw;
new Error();

This is invalid code, because unlike return, throw must be followed by an expression.

To avoid this problem (to prevent ASI), you could use parentheses:

js
throw (
  new Error()
    

Examples

Throwing a user-defined error

This example defines a function that throws a TypeError if the input is not of the expected type.

js
function isNumeric(x) {
  return ["number", "bigint"].includes(typeof x);
function sum(...values) {
  if (!values.every(isNumeric)) {
    throw new TypeError("Can only add numbers");
  return values.reduce((a, b) => a + b);
console.log(sum(1, 2, 3)); // 6
try {
  sum("1", "2");
} catch (e) {
  console.error(e); // TypeError: Can only add numbers
    

Throwing an existing object

This example calls a callback-based async function, and throws an error if the callback receives an error.

js
readFile("foo.txt", (err, data) => {
  if (err) {
    throw err;
  console.log(data);

Errors thrown this way are not catchable by the caller and will cause the program to crash unless (a) the readFile function itself catches the error, or (b) the program is running in a context that catches top-level errors. You can handle errors more naturally by using the Promise() constructor.

js
function readFilePromise(path) {
  return new Promise((resolve, reject) => {
    readFile(path, (err, data) => {
      if (err) {
        reject(err);
      resolve(data);
try {
  const data = await readFilePromise("foo.txt");
  console.log(data);
} catch (err) {
  console.error(err);
    

Specifications

Specification