Error guides

Fix "Unexpected token 'if'" — conditional catch clauses on V8

SyntaxError: Unexpected token 'if'

Why it happens

Rhino supported Mozilla's conditional catch clauses: several catch (e if …) blocks after one try. They were never standard, so V8 refuses to parse the file.

The fix

Use one catch and branch inside it. Re-throw anything you didn't handle — that is what Rhino did when no clause matched.

// Rhino
try {
  var data = JSON.parse(cache);
} catch (e if e instanceof SyntaxError) {
  data = {};
} catch (e if e.message.indexOf('quota') >= 0) {
  Utilities.sleep(1000);
}

// V8
try {
  var data = JSON.parse(cache);
} catch (e) {
  if (e instanceof SyntaxError) {
    data = {};
  } else if (e.message.indexOf('quota') >= 0) {
    Utilities.sleep(1000);
  } else {
    throw e;
  }
}

Gotchas

How V8Shift handles it

The whole chain — including a trailing unconditional catch — is merged into a single catch (e) { if … else if … else … } automatically.

Have a whole project to migrate? Paste your .gs or upload a zip for a free V8 compatibility report — every occurrence of this pattern with file and line, plus an auto-rewritten project.
Related errors