Sentencias de manejo de excepciones

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

Contenido de la revisión

throw Statement

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.PreviousNext("Core_JavaScript_1.5_Guide:Exception_Handling_Statements", "Core_JavaScript_1.5_Guide:Exception_Handling_Statements:try...catch_Statement")}}

{{ wiki.languages( { "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_Statement"> throw Statement </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.PreviousNext("Core_JavaScript_1.5_Guide:Exception_Handling_Statements", "Core_JavaScript_1.5_Guide:Exception_Handling_Statements:try...catch_Statement")}}
</p>
</div>
{{ wiki.languages( { "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