# allow try...catch to return a value

**URL:** <https://es.discourse.group/t/allow-try-catch-to-return-a-value/2060>\
**Category:** 💡 Ideas\
**Created:** [June 11, 2024, 9:39am UTC](https://es.discourse.group/t/allow-try-catch-to-return-a-value/2060 "2024-06-11T09:39:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yukulele](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/yukulele/32/1519_2.png) [@yukulele](https://es.discourse.group/u/yukulele)\
**Post date:** [June 11, 2024, 9:39am UTC](https://es.discourse.group/t/allow-try-catch-to-return-a-value/2060/1 "2024-06-11T09:39:42Z")

</div>

My suggestion is to allow `return` in `try...catch` statements, such that this code:

```js
/** @type { User | false } */
let user
try {
  user = getUser()
} catch (e) {
  user = false
}

```

can be replaced by this one:

```js
const user = try {
  return getUser()
} catch(e) {
  return false
}

```

The braces could be made optional, which would mean an implicit `return` (as for arrow functions):

```js
const user = try getUser() catch(e) false

```

the `try...catch` blocks therefore become statements, but I don't think this will be a problem with older code.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [June 11, 2024, 1:50pm UTC](https://es.discourse.group/t/allow-try-catch-to-return-a-value/2060/2 "2024-06-11T13:50:00Z")

</div>

That (the "by this one" version) already works just fine.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [June 11, 2024, 2:53pm UTC](https://es.discourse.group/t/allow-try-catch-to-return-a-value/2060/3 "2024-06-11T14:53:11Z")

</div>

@ljharb I think you missed the `const user =` part, `try` is not an expression.  
It would need to be

```js
const user = (() => {
  try {
    return getUser();
  } catch(e) {
    return false;
  }
})();

```

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [June 11, 2024, 2:56pm UTC](https://es.discourse.group/t/allow-try-catch-to-return-a-value/2060/4 "2024-06-11T14:56:18Z")

</div>

Related:

- [GitHub - dead-claudia/proposal-try-expression: A proposal to introduce `try` expressions into ECMAScript](https://github.com/dead-claudia/proposal-try-expression)
- [Statements as expressions](https://es.discourse.group/t/statements-as-expressions/894)
- [GitHub - tc39/proposal-do-expressions: Proposal for `do` expressions](https://github.com/tc39/proposal-do-expressions)

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [June 11, 2024, 3:05pm UTC](https://es.discourse.group/t/allow-try-catch-to-return-a-value/2060/5 "2024-06-11T15:05:25Z")

</div>

ah, lol, i did thanks. In that case, see [https://github.com/tc39/proposal-do-expressions](https://github.com/tc39/proposal-do-expressions)
