Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bizarre behavior when throw exceptions without try in a continuation block #14

Open
BYVoid opened this issue Jun 9, 2013 · 1 comment
Labels

Comments

@BYVoid
Copy link
Owner

BYVoid commented Jun 9, 2013

var test = function(callback) {
    console.log('called');
    callback(null);
}

function main() {
    try {
        test(obtain(a));
    } catch (e) {
        console.log('error by obtain', e);
    }
    throw 'a';
}

try {
    main();
} catch (e) {
    console.log('error by main', e);
}

Expected:

called
error by main a

Actual:

called
error by obtain a
error by obtain a
error by main a
@reynir
Copy link
Contributor

reynir commented Oct 4, 2013

The problem seems that 1 try-catch block can generate 2 potentially nested try-catch blocks.

A somewhat hacky solution could be to add a boolean variable that tells whether one of the try-catch blocks have been triggered yet. If it has been triggered then the exception should be re-thrown.

So the example should compile to something like this:

function (_$cont) {
  var _$has_catched = false;
  try {
    test(function (arguments, _$param0, _$param1) {
      try {
        _$err = _$param0;
        a = _$param1;
        if (_$err)
          throw _$err;
        _$cont();
      } catch (_$err) {
        if (_$has_catched)
          throw _$err;
        _$has_catched = true;
        _$cont(_$err);
      }
    }.bind(this, arguments));
  } catch (_$err) {
    if (_$has_catched)
      throw _$err;
    _$has_catched = true;
    _$cont(_$err);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants