March 1999 Draft
JavaScript 2.0
Error Recovery
previousupnext

Wednesday, March 24, 1999

Introduction

Developers often find it desirable to be able to write a single script that takes advantage of the latest features in a host environment such as a browser while at the same time working in older host environments that do not support these features. JavaScript 2.0's error recovery will enable one to easily write such scripts. One may still need to use techniques such as the LANGUAGE HTML attribute to support pre-JavaScript 2.0 environments, but at least the number of such environments that will need to be special-cased will not increase in the future.

Error recovery is a dual of versioning: error recovery lets a script run under a variety of historical hosts, while versioning lets a host run a variety of historical scripts.

Lazy Errors

The main principle of error recovery is that syntax and semantic errors in parts of a program are reported lazily. If a program part that contains an error is never executed, the error never breaks the script. For example, the following function finishes successfully if whizBangFeature is false:

function move(integer x, integer y, integer d) {
  x += 10;
  y += 3;
  if (whizBangFeature) {
    simulate{@x and #y} along path
  } else {
    x += d; y += d;
  }
  return [x,y];
}

The code simulate{@x and #y} along path is a syntax error, but this error does not break the script unless the script attempts to execute that piece of code.

Error recovery is often useful in conjunction with the long version of import statements:

function move(integer x, integer y, integer d) {
  x += 10;
  y += 3;
  import ("WhizBangPackage") {
    var Path p = paths::defaultPath;
    simulate(x,y,p);
  } else {
    x += d; y += d;
  }
  return [x,y];
}

Details

When a syntax or semantic error is discovered in a JavaScript program, the parser skips to the end of the nearest enclosing Block and replaces the whole Block with code that will generate a syntax error if executed. To skip to the end of the Block, the parser balances { and } tokens and ignores all other tokens.

Debugging

Even though syntax and semantic errors break programs lazily, implementations are allowed and encouraged to report these errors early in the form of warnings visible to script developers but not end users. A script author will benefit from knowing about possible syntax errors at compile time rather than at run time.


Waldemar Horwat
Last modified Wednesday, March 24, 1999
previousupnext