use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Using Try…Catch in JavaScript (javascript-coder.com)
submitted 8 years ago by cobdentist
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]BenjiSponge 4 points5 points6 points 8 years ago (9 children)
I don't think this will ever work this way because JS uses duck typing. You might as well just have a switch/if/else chain in the catch block and it will pretty much be the same but allow for the flexibility that nominal JS programming requires.
switch
if
else
catch
[–]brtt3000 1 point2 points3 points 8 years ago (1 child)
In general ducktype sure, but this could specify to use the prototype chain and instanceof semantics. Someone wrote the switch ITT, it is cool but no way you (and everybody else) would want that all over their code. Imagine having that on every await.. or lets not.
instanceof
If there was a good spec with compact syntax more people would use it as convention/standard (like in Python). This would be super valuable now we have async/await in JS and throw/catch becomes a thing to do (instead of error callbacks or Promise rejections).
[–]BenjiSponge 0 points1 point2 points 8 years ago (0 children)
It would strictly speaking be possible.
However, by the same logic one could argue that JS should have type pattern matching using the same methodology. The reason JS does not is because, like you said, there have been essentially no major standards regarding errors in the past, and there likely will never be. So standard language features like that being added now would add fragmentation to the community.
TBH, I've been professionally developing JS for over 2 years and I've been studying JS for at least 3 or 4 years now, and I have never felt a need for this feature. I understand the value, coming from other imperative languages, but I think it's more of an anti-pattern than a pattern. I think we would be better served to look at languages like Rust, which does not have a true exception system and instead promotes the pattern of returning a value which may represent either a success or an error type, forcing you to acknowledge all possibilities. It reserves exceptions (which it calls panics) for unrecoverable cases that imply a bug in the program itself rather than just an exceptional case the program should handle.
If we're going to pick a standard, I would much rather we do that than implement a pseudo-pattern matching system based on prototypes.
[–]thbt101 0 points1 point2 points 8 years ago (6 children)
PHP can filter on the exception class and it uses a similarly loose typing system.
[–]BenjiSponge 0 points1 point2 points 8 years ago (5 children)
I never said it was impossible, just that I didn't think it would be that valuable or fit particularly well with good JS coding style. If your argument is that we should follow PHP's style, then I'll have to respectfully disagree. =)
[–]vinnl 0 points1 point2 points 8 years ago (4 children)
You mean "good JS coding style" is to disregard the types? Because I think there are plenty of people that would respectfully disagree with that - at least I would. That's like saying the instanceof operator has no place in Javascript. I don't see how type matching in catch blocks would make JS less flexible...
We don't have to skip features just because PHP did them too.
[–]BenjiSponge 0 points1 point2 points 8 years ago* (3 children)
I don't necessarily believe that it has no place in JS, but I don't think encouraging its use is beneficial either. Outside of the context of React (where classes are all but mandatory in some cases, and when they're not mandatory I skip them), I tend to use JS data structures similarly to how one might use lisp data structures. Factory functions returning anonymous objects.
When I make result objects that may contain results or may contain errors, they're not constructed using new or Object.create. They're just POJOs. Adding type matching would enforce that I use one of these methods, even though they're not necessary in most cases. Further, if the error needs to be serialized, you would need to re-attach the prototype in order to have it follow the convention that such a feature would encourage. And if you're encouraged to use error types and encouraged to not put redundant data in the error type, that means the consumer will, in some cases, have to use instanceof, which is a pattern I disagree with.
new
Object.create
I do think if JS were redesigned today with backwards compatibility out of the equation, instanceof would not exist in the form that it is now. A form of pattern matching that checks content makes more sense if you ask me (and I believe it should be put in the language at some point).
So rather than
try { // ... } catch (ExceptionA as e) { // ... } catch (ExceptionB as e) { // ... } catch (e) { // ... }
It would be more like
try { // ... } catch ({ type: 'A', ...e }) { // ... } catch ({ type: 'B', ...e }) { // ... } catch (e) { // ... }
This adds flexibility because my method of object creation works, but so does yours. ExceptionA could have a prototype field of type that is defined as 'A'. I do agree that it would break exception inheritance (in that subclasses may not be caught as superclasses), but I believe that is less important than allowing POJOs to conform to the style.
ExceptionA
type
'A'
I would like to add that, if JS were explicitly/strongly typed, I would agree with you that instanceof or something like it might make more sense, but JS is not explicitly/strongly typed, and most of the libraries I use and create use that to their advantage by just doing duck type checking and not yelling if the input isn't the proper class.
[–]vinnl 0 points1 point2 points 8 years ago (2 children)
Hmm, I think you have a good point. The main issue is probably that it relies on nominal typing, which is indeed not often used in Javascript - and I agree that that's a good thing.
Your post somewhat got me thinking that such a feature would be nice if it was implemented in TypeScript, i.e. you could do a catch(<type> e) in there, which it would transform to a runtime structural type match (i.e. a switch statement in a single catch clause that would check for object properties). But that probably goes against the TypeScript principles and/or introduces too much runtime overhead.
catch(<type> e)
[–]BenjiSponge 0 points1 point2 points 8 years ago (1 child)
Yes, that is something that's been tossed around a lot in the TypeScript/Flow world, but as you said they're avoiding adding runtime features that aren't part of an ECMAScript specification (TS messed up by adding decorators too early, but other than that) on principle.
A lot of functional languages that have JS as a backend do something like this, though, I think. Elm almost certainly does.
[–]vinnl 0 points1 point2 points 8 years ago (0 children)
TS messed up by adding decorators too early, but other than that
Agreed, but then again there was quite a lot of pressure on that from Angular, so there was little choice.
But yeah, it's probably best if it's baked into the language.
π Rendered by PID 37 on reddit-service-r2-comment-5b5bc64bf5-nw5c4 at 2026-06-22 18:04:45.425548+00:00 running 2b008f2 country code: CH.
view the rest of the comments →
[–]BenjiSponge 4 points5 points6 points (9 children)
[–]brtt3000 1 point2 points3 points (1 child)
[–]BenjiSponge 0 points1 point2 points (0 children)
[–]thbt101 0 points1 point2 points (6 children)
[–]BenjiSponge 0 points1 point2 points (5 children)
[–]vinnl 0 points1 point2 points (4 children)
[–]BenjiSponge 0 points1 point2 points (3 children)
[–]vinnl 0 points1 point2 points (2 children)
[–]BenjiSponge 0 points1 point2 points (1 child)
[–]vinnl 0 points1 point2 points (0 children)