0

While working on some projects, i've found out that when i split functionality between functions, it becomes difficult to find out, which functions can throw exception.

I can add comment to function, or add some info in jsdoc, but the problem is that function may called by another function, and so on. At this point if there's many of them, i need to check every function to determine if it potentially can/can't throw exception.

Is there anything that can help with that? Maby not in JS, but in any languages. What is that called?

What i imagine myself would be helpful is if you could add some kind of tag on function that can throw.
And then, by default, it propagates this tag to every function that uses it.
So, every function has then list of tagged functions that are used by it
And every function (whether automatically, by try/catch wrap detection, or manually) can stop further propagation of tag.

Such feature would also need support of code editor of course

Rostys
  • 39
  • 1
  • 5
  • Java and other languages have explicit, required annotations for this on functions. Javascript alas does not… – deceze Aug 31 '23 at 02:56
  • It's called a https://en.wikipedia.org/wiki/Checked_exception and is part of some static type systems (which JS doesn't have of course) – Bergi Aug 31 '23 at 02:56
  • 1
    Documentation (e.g. with [`@throws`](https://jsdoc.app/tags-throws.html) doc comments) with careful review goes a long way. Alternatively, don't use exceptions but instead return `Result` objects which force the caller to handle (and optimally propagate) errors; this can become a bit verbose but has good TypeScript and linting support. – Bergi Aug 31 '23 at 03:00

0 Answers0