Like many other programming languages, JavaScript provides for throwing and catching exceptions. JavaScript supports the standard try...catch...finally statement: try { doSomething(); } catch (e) { window.alert(e.description); } finally { cleanUp(); } JavaScript also supports the throw statement. Any type of object can be thrown: throw "error!"; throw {name:"anError", description:"an error occurred"}; The browser creates and throws errors under certain circumstances. For example, trying to access a field on a variable that is undefined will raise an error. Trying to call a function that does not exist also raises an error. These types of exceptions can also be caught with the try...catch statement. When an exception is raised and isn't caught, the current call stack is unwound and the browser receives an error event. Execution of JavaScript code continues when the next stack is created by a timeout event or by user interaction. |
Contents
|
