JavaScript: Difference between revisions

Content deleted Content added
Robmv (talk | contribs)
m oops wrong ___location
Line 332:
 
===[[Error handling]]===
JavaScript has acquired a notorious reputation among programmers for its idiosyncratic method of error handling. Since errors in JavaScript only appear in run-time (i.e., there is no way to check for errors without executing the code), and since JavaScript is interpreted by the web browser as the page is viewed, it is extremely difficult for JavaScript to generate a useful error message. Some actual examples:
Newer versions of JavaScript (as used in [[Internet Explorer]] 5 and [[Netscape (web browser)|Netscape]] 6) include a <tt>try ... catch</tt> error handling statement.
 
<code>Error: Object expected. Line: unknown.</code> (this is comparatively helpful)
<code>Error: Unknown Syntax Error. Line: 1.</code> (there were no problems on line 1)
<code>Error: ||| Line: unknown. </code> (really!)
 
In addition, simple errors in syntax tend to generate complex, unrelated error messages, so that deciphering a simple misplaced quotation mark can require many executions and tests to find the correct place.
Newer versions of JavaScript (as used in [[Internet Explorer]] 5 and [[Netscape (web browser)|Netscape]] 6) include a <tt>try ... catch</tt> error handling statement. Purloined from [[Java]], this is intended to help with run-time errors but does so with mixed results.
 
The <code>try ... catch ... finally</code> statement catches [[exception]]s resulting from an error or a [[throw statement]]. Its syntax is as follows: