Just try

# Michael McGlothlin (9 years ago)

It'd be nice if you could just do try {} without all the catch and finally stuff because about half the time the logic is simpler if I can just put all the error handling code in one place at the end. I end up with a lot of empty catch (err){} laying around waiting to break something. And using one large try/catch/finally block isn't really workable either because I don't want an exception to break the flow.

Of course it'd be nice if there was syntax to shorten

let test = undefined; try { test = testSomething(); } catch ( err ) {}

into just

let test = try testSomething() || undefined;

Possibly where without the || undefined test would contain an Error instance that's flagged as thrown or such. An Error flagged as thrown should evaluate as false so that logic ops work.

# Mohsen Azimi (9 years ago)

This would be nice with await but how would you specify the boundary?

let json = try await (await fetch('file'.json).json());

should that throw on .json() now?

# Michał Wadas (9 years ago)

Syntax sugar similar to Go error handling would be much more useful, at least for me :

let [err, result] = try JSON.parse('invalid');

# Alican Çubukçuoğlu (9 years ago)

The recommended way of checking for file existence in Node.js is calling fs.stat() on the file. If no error was passed to the callback, it exists. If "ENOENT" was passed, it doesn't.

If you "promisify" fs.stat() and use it with async/await, it throws when a file doesn't exist so you end up writing a lot of try/catches. You say "Hey, it would be great if I didn't have to keep writing catch(e){}." but what if the error wasn't "ENOENT"?

That's why I withdrew myself from suggesting such a thing. Carelessly silencing errors is no good.

I like the idea of let stuff = try something() putting the error in stuff but the problem is you can throw strings in JavaScript:

function getUserName() {
  throw 'Error';
}

const userName = try getUserName();

if (userName instanceof Error) {
  handleError(userName);

  return;
}

console.log('There was no error, yay!');
// Actually there was

Also try stuff() || undefined will not be evaluated to undefined on error:

> var err = new Error('Stuff blew up!');

undefined
> err || undefined
[Error: Stuff blew up!]
# Dean Landolt (9 years ago)

On Fri, Oct 30, 2015 at 2:17 PM, Alican Çubukçuoğlu < alicancubukcuoglu at gmail.com> wrote:

The recommended way of checking for file existence in Node.js is calling fs.stat() on the file. If no error was passed to the callback, it exists. If "ENOENT" was passed, it doesn't.

If you "promisify" fs.stat() and use it with async/await, it throws when a file doesn't exist so you end up writing a lot of try/catches. You say "Hey, it would be great if I didn't have to keep writing catch(e){}." but what if the error wasn't "ENOENT"?

That's why I withdrew myself from suggesting such a thing. Carelessly silencing errors is no good.

I like the idea of let stuff = try something() putting the error in stuff but the problem is you can throw strings in JavaScript:

function getUserName() {
  throw 'Error';
}

const userName = try getUserName();

if (userName instanceof Error) {
  handleError(userName);

  return;
}

console.log('There was no error, yay!');
// Actually there was

I think you missed the tuple destructuring in the example try-expression. It wasn't:

let errOrValue = try JSON.parse('invalid');

But instead:

let [err, result] = try JSON.parse('invalid');

This avoids the problem you noted, and actually, plays nice w/ node's error-first callbacks:

cb.apply(null, try JSON.parse('invalid'));