Visit Mozilla.org

Referencia de JavaScript 1.5:Objetos globales:Error

De MDC


Tabla de contenidos

[editar] Resumen

Objeto del Núcleo

Representa un error en tiempo de ejecución.

[editar] Se Crea Por

El constructor Error:

new Error()
new Error(mensaje)

Los errores en tiempo de ejecución también tiene como resultado un nuevo objeto Error que es creado y lanzado.

[editar] Parámetros

mensaje 
Mensaje de error.

[editar] Descripción

Además del Error base, existen otros seis tipos de error en el núcleo de JavaScript 1.5:

  • EvalError: lanzado cuando un error ocurre al ejecutar código en eval()
  • RangeError: lanzado cuando una variable numérica o un parámetro se encuentra fuera de su rango válido
  • ReferenceError: lanzado cuando se referencia una referencia no válida
  • SyntaxError: lanzado cuando ocurre un error de sintaxis mientras se traduce (parsea) código en eval()
  • TypeError: lanzado cuando una variable o un parámetro no es del tipo válido
  • URIError: lanzado cuando encodeURI() o decodeURI() se le pasan parámetros no válidos

[editar] Properties

  • constructor: Specifies the function that creates an object's prototype.
  • description: Error description/message (IE only).
  • fileName: Path to file that raised this error (Mozilla only).
  • lineNumber: Line number in file that raised this error (Mozilla only).
  • message: Error message.
  • name: Error name.
  • number: Error number (IE only).
  • prototype: Allows the addition of properties to an Error object.
  • stack: Stack trace (Mozilla only).

[editar] Ejemplos

[editar] Ejemplo: Lanzando un error genérico

Usually you create an Error object with the intention of raising it using the throw keyword. You can handle the error using the try...catch construct:

try {
    throw new Error("Whoops!");
} catch (e) {
    alert(e.name + ": " + e.message);
}

[editar] Ejemplo: Manejando un error específico

You can choose to handle only specific error types by testing the error type with the error's constructor property or, if you're writing for modern JavaScript engines, instanceof keyword:

try {
    foo.bar();
} catch (e) {
    if (e instanceof EvalError) {
        alert(e.name + ": " + e.message);
    } else if (e instanceof RangeError) {
        alert(e.name + ": " + e.message);
    }
    // ... etc
}

[editar] Vea También