Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Generator throw() Method

JavaScript Generator throw() Method

JavaScript Generator.prototype.throw() method is an inbuilt method in JavaScript that is used to resume the execution of a generator by throwing an error into it.

Syntax: 

gen.throw(exception);

Parameters: This function accepts a single parameter as mentioned above and described below: 

  • exception: This parameter holds the exception to be thrown.

Return value: This method returns an Object containing two properties: 

  • done: It has the value 
    • true – for the iterator which is past the end of the iterated sequence.
    • false – for the iterator which is able to produce the next value in the sequence.
  • value: It contains any JavaScript value which is returned by the iterator.

Below examples illustrate the Generator.prototype.throw() method are listed below:

Example 1: This example shows the use of the Generator.prototype.throw() method in Javascript.

javascript




function* GFG() {
    while (true) {
        try {
            yield "Null";
        } catch (e) {
            console.log('Generator.prototype.throw()');
        }
    }
}
  
const neveropen = GFG();
console.log(neveropen.next());
console.log(neveropen.throw(new Error('Error caught!')));


Output: 

Object { value: "Null", done: false }
"Generator.prototype.throw()"
Object { value: "Null", done: false }

Example 2: This example shows the use of the Generator.prototype.throw() method in Javascript.

javascript




function* GFG(pageSize = 1, list) {
    let output = [];
    let index = 0;
  
    while (index < list.length) {
        try {
            output = [];
            for (let i = index; i < index + pageSize; i++) {
                if (list[i]) {
                    output.push(list[i]);
                }
            }
  
            yield output;
            index += pageSize;
        } catch (e) {
            console.log('Generator.prototype.throw()');
        }
    }
}
list = [1, 2, 3, 4, 5, 6, 7, 8]
let geek = GFG(3, list);
  
console.log(geek.next());
console.log(geek.next());
console.log(geek.next());
console.log(geek.throw(new Error('Error caught!')));


Output: 

Object { value: Array [1, 2, 3], done: false }
Object { value: Array [4, 5, 6], done: false }
Object { value: Array [7, 8], done: false }
"Generator.prototype.throw()"
Object { value: Array [7, 8], done: false }

Supported Browsers: The browsers supported by Generator.prototype.throw() method are listed below: 

  • Google Chrome 39 and above
  • Firefox 26 and above
  • Opera 26 and above
  • Safari 10 and above
  • Edge 13 and above

We have a complete list of Javascript Generator methods, to check those please go through the Javascript Generator Reference article.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments