The catch with try...catch

http://weblog.bocoup.com/ http://benalman.com/

by Ben Alman

JavaScript

var results = [];
var e = 1;

// Since `x` is undeclared, this throws an exception.
try { x } catch(e) { }

// 1. In IE, e is the exception object. In other browsers, e is 1.
results.push(e);

function test() {
  // Since `x` is undeclared, this throws an exception.
  try { x } catch(e) { }

  // 2. In IE, e is the exception object. In other browsers, e is 1.
  results.push(e);

  // This should change the global `e`, but it doesn't in IE.
  e = 2;
}

test();

// 3. In IE, e is the exception object. In other browsers, e is 2.
results.push(e);

// The should output 1,1,2
document.write(results);