Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 291929 of throw

  • Enlace amigable (slug) de la revisión: Guía_JavaScript_1.5/Sentencias_de_manejo_de_excepciones/throw
  • Título de la revisión: throw
  • Id de la revisión: 291929
  • Creada:
  • Creador: Nathymig
  • ¿Es la revisión actual? No
  • Comentario

Contenido de la revisión

throw

Use the throw statement to throw an exception. When you throw an exception, you specify an expression containing the value of the exception:

throw expression

The following code throws several exceptions.

throw "Error2";    // generates an exception with a string value
throw 42;          // generates an exception with the value 42
throw true;        // generates an exception with the value true

You can specify an object when you throw an exception. You can then reference the object's properties in the catch block. The following example creates an object myUserException of type UserException and uses it in a throw statement.

// Create an object type UserException
function UserException (message) {
   this.message=message;
   this.name="UserException";
}
// Create an instance of the object type and throw it
myUserException=new UserException("Value too high");
throw myUserException;

{{template.AnteriorSiguiente("Guía JavaScript 1.5:Sentencias de manejo de excepciones", "Guía_JavaScript_1.5:Sentencias de manejo de excepciones:try...catch")}}

Categorías

enlaces interwikis

{{ wiki.languages( { "en": "en/Core_JavaScript_1.5_Guide/Exception_Handling_Statements/throw_Statement", "fr": "fr/Guide_JavaScript_1.5/Instructions_de_gestion_d\'exceptions/L\'instruction_throw", "ja": "ja/Core_JavaScript_1.5_Guide/Exception_Handling_Statements/throw_Statement", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Instrukcje_obs\u0142ugi_wyj\u0105tk\u00f3w/Instrukcja_throw" } ) }}

Fuente de la revisión

<div class="noinclude"></div>
<h3 name="throw"> throw </h3>
<p>Use the <code>throw</code> statement to throw an exception. When you throw an exception, you specify an expression containing the value of the exception:
</p>
<pre>throw expression
</pre>
<p>The following code throws several exceptions.
</p>
<pre>throw "Error2";    // generates an exception with a string value
throw 42;          // generates an exception with the value 42
throw true;        // generates an exception with the value true
</pre>
<p>You can specify an object when you throw an exception. You can then reference the object's properties in the <code>catch</code> block. The following example creates an object <code>myUserException</code> of type <code>UserException</code> and uses it in a throw statement.
</p>
<pre>// Create an object type UserException
function UserException (message) {
   this.message=message;
   this.name="UserException";
}
// Create an instance of the object type and throw it
myUserException=new UserException("Value too high");
throw myUserException;
</pre>
<div class="noinclude">
<p>{{template.AnteriorSiguiente("Guía JavaScript 1.5:Sentencias de manejo de excepciones", "Guía_JavaScript_1.5:Sentencias de manejo de excepciones:try...catch")}}
</p><p><span class="comment">Categorías</span>
</p><p><span class="comment">enlaces interwikis</span>
</p>
</div>
{{ wiki.languages( { "en": "en/Core_JavaScript_1.5_Guide/Exception_Handling_Statements/throw_Statement", "fr": "fr/Guide_JavaScript_1.5/Instructions_de_gestion_d\'exceptions/L\'instruction_throw", "ja": "ja/Core_JavaScript_1.5_Guide/Exception_Handling_Statements/throw_Statement", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Instrukcje_obs\u0142ugi_wyj\u0105tk\u00f3w/Instrukcja_throw" } ) }}
Revertir a esta revisión