Visit Mozilla.org

Dokumentacja języka JavaScript 1.5:Obiekty:Error

z Mozilla Developer Center, polskiego centrum programistów Mozilli.

UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...

Spis treści

[edytuj] Podsumowanie

Obiekt główny

Represents a runtime error.

[edytuj] Tworzone przez

Konstruktor Error:

new Error()
new Error(message)

Runtime errors also result in a new Error object being created and thrown.

[edytuj] Parametry

message 
Wiadomość błędu.

[edytuj] Opis

Besides the base Error, there are six other core error types in JavaScript 1.5:

  • EvalError: raised when an error occurs executing code in eval()
  • RangeError: raised when a numeric variable or parameter is outside of its valid range
  • ReferenceError: raised when de-referencing an invalid reference
  • SyntaxError: raised when a syntax error occurs while parsing code in eval()
  • TypeError: raised when a variable or parameter is not a valid type
  • URIError: raised when encodeURI() or decodeURI() are passed invalid parameters

[edytuj] Własności

  • 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).

[edytuj] Przykłady

[edytuj] Przykład: Throwing a generic error

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);
}

[edytuj] Przykład: Handling a specific error

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);
    }
    // ... itd.
}

[edytuj] Zobacz także

throw, try...catch