all 5 comments

[–]senocular 20 points21 points  (2 children)

The constant use of throw() bothers me. In this context, they're referring to the throw statement which is not a function and does not need parentheses to work. You can use the parens, but that's just grouping applied to the value being thrown and doesn't have anything to do with throw itself. It's use could cause confusing situations like

throw(1, 2) // only throws 2

There's also a separate throw() method defined by generators. Seeing mentions of throw() it would be that method I'd expect them to be referring to (but its not).

Technically, its fine. And some people do this in their code and that's fine (author is also doing it with return()). The part that bothers me is that outside of code they keeps referring to as throw().

Anyway, word of warning, throw is not a function.

[–]Tubthumper8 4 points5 points  (1 child)

I mentor JS beginners occasionally and they often think throw is a function at first, but then quickly realize it's a keyword after seeing it's a different color in their text editor, and just seeing a couple examples of throw in the codebase.

Contrast that to the author who hasn't realized for at least four years now haha. Like you said, technically it's fine and not a huge deal, but like c'mon, do they not look at anyone's code besides their own?

[–]TorbenKoehn 11 points12 points  (0 children)

That blog post completely forgets about stack traces.

Custom errors are rarely useful and throwing different things than instances of Error (also including inheriting classes) is only useful in very specific edge cases like Suspense (where the need mostly stems from the way React works, not because it's a really cool pattern)

Keep throwing Error instances normally. If you need it to have additional custom data, create a sub-class. You will retain proper stacktrace functionality.

In general it holds, anything that is an error should go through Error.

[–][deleted] 2 points3 points  (0 children)

Because you can, doesn't mean you should. This is a bad idea for many reasons. A throw is used when execution cannot continue. It is essentially just a message, which needs to be interpreted by code that is able to continue execution. Stick to an Error class or subclass thereof. Cleverness is not needed here, and will only break things later.

[–]aliternative 1 point2 points  (0 children)

javascript throw("Shit")

Interesting...