# Explicit exceptions - a solution to fragile code dealing with exceptions

**URL:** <https://es.discourse.group/t/explicit-exceptions-a-solution-to-fragile-code-dealing-with-exceptions/796>\
**Category:** 💡 Ideas\
**Created:** [May 18, 2021, 3:58pm UTC](https://es.discourse.group/t/explicit-exceptions-a-solution-to-fragile-code-dealing-with-exceptions/796 "2021-05-18T15:58:17Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [May 19, 2021, 1:56am UTC](https://es.discourse.group/t/explicit-exceptions-a-solution-to-fragile-code-dealing-with-exceptions/796/6 "2021-05-19T01:56:50Z")

</div>

Alright, I think I'm understanding your vision better. We declare which exception types a particular function might throw. Anything not in its signature will automatically be escalated. Then, every time we call one of these exception-throwing functions, we use a try-catch, and explicitly indicate how we would deal with each type of exception (will we handle it? propagate it? escalate it?)

I can see that being able to solve the problems just as much as my original proposal, as long as a try-catch is used every time an exception-throwing function is called. In order for this try-catch concept to fully work, we would need to expose some easy way for the user to escalate the exception (as they can't just do it by simply omitting the exception from the `excepts` clause like in my original proposal). So, something like this:

```javascript
function getUserProperty(userId, propName) throws 'NotFound' {
  try {
    return getUser(userId)[propName]
  } catch 'NotFound' (ex) {
    throw ex // propagate this exception
  } catch 'SomethingElse' (ex) {
    throw Error.fromException(ex) // Escalate the exception.
  }
}

```

> [@Clemdz](#):
>
> The use of a try-catch around a function call, that has declared its expected exceptions in its definition, could even be enforced by using a linter, in order to ensure that every expected exception is always handled.

This, unfortunately, can only be enforced to a degree by a linter. Code like this would cause issues:

```javascript
function doSomething(fn) {
  fn() // Is a try-catch required here? Or not?
}

```

For the discussion on adding new syntax for exception handling, the error catching pipe operator "|^" that we've been discussing in [this thread](https://es.discourse.group/t/error-dejection-operator/788) along with the [pattern matching proposal](https://github.com/tc39/proposal-pattern-matching) would make for a really elagent solution:

```javascript
function getUserProperty(userId, propName) throws 'NotFound' {
  const user = getUser(userId) |^ match(?) {
    // Propagates NotFound
    when({ code: 'NotFound', ..._ }) { throw ? }
    // Escalates any other exception
    else if (? instanceof Exception) { throw Error.fromException(?) }
    // Let all other fatal errors pass through
    else { throw ? }
  }
  return user[propName]
}

```

---

_[View the full topic](https://es.discourse.group/t/explicit-exceptions-a-solution-to-fragile-code-dealing-with-exceptions/796)._
