JavaScript syntax: Difference between revisions

Content deleted Content added
m Date: Months corrected: 2→3
Nelsonkam (talk | contribs)
Exception handling: Changed formatting.
Tags: Mobile edit Mobile web edit
Line 1,405:
 
==Exception handling==
JavaScript includes a <code>try … catch … finally</code> [[exception handling]] statement to handle run-time errors.
 
The <code>try … catch … finally</code> statement catches [[exception handling|exceptions]] resulting from an error or a [[throw statement]]. Its syntax is as follows:
 
<syntaxhighlight lang=JavaScript>
Line 1,419:
</syntaxhighlight>
 
Initially, the statements within the try block execute. If an exception is thrown, the script's control flow immediately transfers to the statements in the catch block, with the exception available as the error argument. Otherwise the catch block is skipped. The catch block can <tt>{{mono|throw(errorValue)</tt>}}, if it does not want to handle a specific error.
 
In any case the statements in the finally block are always executed. This can be used to free resources, although memory is automatically garbage collected.
Line 1,435:
</syntaxhighlight>
 
In a browser, the <tt>{{mono|onerror</tt>}} event is more commonly used to trap exceptions.
<!-- This needs verification -->