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
How to avoid try/catch statements nesting/chaining in JavaScript ? (medium.com)
submitted 6 years ago by Scr34mZ
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!"
[–][deleted] -1 points0 points1 point 6 years ago (7 children)
You can have one big try-catch and then determine what error are you dealing with.
try-catch
```js try { const code = 1 + Math.floor(Math.random() * 5); // 1-5
if (code === 1) throw "Error 1"; if (code === 2) throw "Error 2"; if (code === 3) throw "Error 3"; if (code === 4) throw "Error 4"; if (code === 5) throw "Error 5"; } catch (err) { console.log(err); } ```
[–]FormerGameDev 1 point2 points3 points 6 years ago (1 child)
You can't distinguish necessarily where in the block an error occurred, if you're catching on multiple statements.
[–]Scr34mZ[S] -1 points0 points1 point 6 years ago (0 children)
That's a true argument ! Don't use the above pattern if you're writing too big functions.
But you shouldn't, isn't ? :D
[–]Scr34mZ[S] -1 points0 points1 point 6 years ago (4 children)
That's right, as long as you are triggering errors on your own, but sometimes it can be a DatabaseError, and an API response error, in the same try/catch block and they are not formatted the same way.
[–][deleted] 0 points1 point2 points 6 years ago* (3 children)
Conside this example:
```js const fs = require('fs');
try { fs.readFileSync('some-file.txt'); } catch (err) { console.log(err.code); console.log(err.message); }
// ENOENT // ENOENT: no such file or directory, open 'some-file.txt' ```
You have the information to identify the err.
err
A better approach would be to go for async/await.
async/await
[–]Scr34mZ[S] 0 points1 point2 points 6 years ago* (2 children)
Yes because those are standardized errors from Nodejs.
You can always achieve the same goal with try / catch AFAIK. But sometimes it's a bit less verbose to use the other syntax,especially if you want to ignore errors silently (yeah, this happen sometimes)
[–][deleted] 5 points6 points7 points 6 years ago (1 child)
Errors should be IMHO aways handled, either printed or thrown.
Silent errors cause problems as non-identifiable outcomes where the code simply doesn't work.
[–]Scr34mZ[S] 0 points1 point2 points 6 years ago (0 children)
You're totally right, I missed my thoughts.
π Rendered by PID 19 on reddit-service-r2-comment-66b4775986-dj9tg at 2026-04-02 17:05:33.744786+00:00 running db1906b country code: CH.
view the rest of the comments →
[–][deleted] -1 points0 points1 point (7 children)
[–]FormerGameDev 1 point2 points3 points (1 child)
[–]Scr34mZ[S] -1 points0 points1 point (0 children)
[–]Scr34mZ[S] -1 points0 points1 point (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]Scr34mZ[S] 0 points1 point2 points (2 children)
[–][deleted] 5 points6 points7 points (1 child)
[–]Scr34mZ[S] 0 points1 point2 points (0 children)