February 1999 Draft
JavaScript 2.0
Error Recovery
previousupnext

Thursday, February 11, 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 enables one to easily write such scripts.

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(int x, int y, int 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(int x, int y, int 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 Thursday, February 11, 1999
previousupnext