| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Generator/throw | [Back] [Original] |
Get to know MDN better
Esta pgina ha sido traducida del ingls por la comunidad. Aprende ms y nete a la comunidad de MDN Web Docs.
This feature is well established and works across many devices and browser versions. Its been available across browsers since septiembre de 2016.
El mtodo throw() reanuda la ejecucin de un generador al lanzar un error en ste y regresar un objeto con las dos propiedades done y value.
gen.throw(excepcin)
excepcinLa excepcin a lanzar. Al hacer depuracin, es til que la excepcin cumpla con instanceof Error.
Un Object con dos propiedades:
done (booleano)
verdadero si el iterador ya lleg al final de la secuencia. En este caso valor define opcionalmente el valor de retorno del iterador.falso si el iterador puede dar un siguiente valor en la secuencia. Es equivalente a no definir la propiedad done.value - cualquier valor Javascript regresado por el iterador. ste puede ser omitido si done is verdadero.
throw()The following example shows a simple generator and an error that is thrown using the throw method. An error can be caught by a try...catch block as usual.
function* gen() {
while (true) {
try {
yield 42;
} catch (e) {
console.log("Error caught!");
}
}
}
var g = gen();
g.next();
// { value: 42, done: false }
g.throw(new Error("Something went wrong"));
// "Error caught!"
// { value: 42, done: false }
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-generator.prototype.throw |
This page was last modified on 24 jun 2025 by MDN contributors.
GeneratorIteratorObject/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |