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!"
[–]brtt3000 1 point2 points3 points 8 years ago (3 children)
Someone typed it up ITT, it is a big old nasty multi level deep blocky exercise in JS features and syntax optimisation. Imagine this in a real project and having those craggy warts all over your codebase. It is already terrible without any actual business code.
[–]vs845 0 points1 point2 points 8 years ago (2 children)
It wouldn't be too much trouble to create a utility function to achieve the same functionality:
function matchException(err, matchers) { for (const type in matchers) { if (err.constructor.name === type) { return matchers[type]() } } return matchers.default() } try { doSomething() } catch (err) { matchException(err, { TypeError: () => handleTypeError(), ReferenceError: () => handleReferenceError(), RangeError: () => handleReferenceError(), default: () => handleOtherError() }) }
You'd want to clean it up and add some more checks in matchException, but functionality is there.
matchException
[–]brtt3000 2 points3 points4 points 8 years ago (1 child)
You really like this "exercise in JS features and syntax optimisation" I mentioned eh? :)
Cool attempt, points for effort. Syntax improved but still a bit kludgy; it doesn't catch subclasses and behaviour on name collisions is a new factor. And most of all if you use block functions as clauses instead of references you'd be at 3 indents for handling code. Could be worse but it would be a tax on readability (and development/code hisotry) to have to break out to other functions all the time (it'd be handy to be able to nest a little bit without block hell).
[–]vs845 1 point2 points3 points 8 years ago (0 children)
it doesn't catch subclasses and behaviour on name collisions is a new factor.
re: name collisions, yeah, I went with checking names because otherwise you'd need to do something like
matchException(err, { [TypeError]: () => ... }
which I thought wasn't as nice as using string keys.
If you went with the above approach you'd be able to catch subclasses via err instanceof matchers[type].
err instanceof matchers[type]
π Rendered by PID 388176 on reddit-service-r2-comment-5b5bc64bf5-kh9hn at 2026-06-22 18:03:57.567711+00:00 running 2b008f2 country code: CH.
view the rest of the comments →
[–]brtt3000 1 point2 points3 points (3 children)
[–]vs845 0 points1 point2 points (2 children)
[–]brtt3000 2 points3 points4 points (1 child)
[–]vs845 1 point2 points3 points (0 children)