• Skip to main content
  • Skip to search
  • Skip to select language
HTML

Structure of content on the web

  • Web APIs

    Interfaces for building web applications

  • Learn
    • CSS

      Learn to style content using CSS

    • Overview

      A customized MDN experience

    • FAQ

      Frequently asked questions about MDN Plus

  • HTTP Observatory

    Scan a website for free

  • JavaScript
  • throw()
      • Deutsch
      • Español
      • Français
      • 日本語
      • 한국어
      • Русский
      • 中文 (简体)

    In this article

    • Syntax
    • Description
    • Examples
    • Specifications
    • Browser compatibility
    • See also
    1. return()
    2. from()
  • Instance methods
    1. find()
    2. reduce()
    3. [Symbol.iterator]()
  • call()
  • displayName Non-standard
  • arguments Non-standard Deprecated
  • caller Non-standard Deprecated
  • Instance methods
    1. __defineGetter__() Deprecated
    2. __defineSetter__() Deprecated
    3. __lookupGetter__() Deprecated
    4. __lookupSetter__() Deprecated
    5. toLocaleString()
    6. __proto__ Deprecated
    7. Generator instances acts as if a throw statement is inserted in the generator's body at the current suspended position, which informs the generator of an error condition and allows it to handle the error, or perform cleanup and close itself.

  • Syntax

    js
    generatorInstance.throw(exception)
    

    Parameters

    exception

    The exception to throw. For debugging purposes, it is useful to make it an instanceof Error.

    Return value

    If the thrown exception is caught by a Object with two properties:

    done

    A boolean value:

    • true if the generator function's control flow has reached the end.
    • false if the generator function is able to produce more values.
    value

    The value yielded from the next yield expression.

    Exceptions

    TypeError

    Thrown if the generator is already running.

    If the exception is not caught by a try...catch within the generator function, it is also thrown to the caller of throw().

    Description

    The throw() method, when called, can be seen as if a throw exception; statement is inserted in the generator's body at the current suspended position, where exception is the exception passed to the throw() method. Therefore, in a typical flow, calling throw(exception) will cause the generator to throw. However, if the yield expression is wrapped in a try...catch block, the error may be caught and control flow can either resume after error handling, or exit gracefully.

    Examples

    Using throw()

    The following example shows a generator and an error that is thrown using the throw method. An error can be caught by a try...catch block as usual.

    js
    function* gen() {
      while (true) {
        try {
          yield 42;
        } catch (e) {
          console.log("Error caught!");
        }
      }
    }
    
    const g = gen();
    g.next();
    / { value: 42, done: false }
    g.throw(new Error("Something went wrong"));
    / "Error caught!"
    / { value: 42, done: false }
    

    Specifications

    Specification
    ECMAScript® 2026 Language Specification
    # sec-generator.prototype.throw

    Browser compatibility

    See also

    • function*

    Help improve MDN

    function*) create iterators that return multiple values, one after another, on-demand.","name":"Generators"}},"browserCompat":["javascript.builtins.Generator.throw"],"pageType":"javascript-instance-method"}}

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant