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