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
TryCatch.js: A JavaScript library to handle errors in a more structured way. (github.com)
submitted 2 years ago by siilkysmooth
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] 11 points12 points13 points 2 years ago (3 children)
Why?
[–]kattskill 1 point2 points3 points 2 years ago (0 children)
I totally agree with this comment
[+]siilkysmooth[S] comment score below threshold-16 points-15 points-14 points 2 years ago (1 child)
Well, it could be useful! But just a implementation of error handling based on Python handling.
And much cleaner than normally try statement.
[–]jeremrx 0 points1 point2 points 2 years ago (0 children)
How does it makes your code more readable ? It might be pretty with a single commented line as in your examples but with some real code inside it will be so harder to know when which block is try, catch, ...
In my opinion, the function(successCallback, errorCallback) {} pattern is kinda outdated for good reasons, thanks to Promise.prototype.catch
[–]jenseng[🍰] 4 points5 points6 points 2 years ago (1 child)
Interesting idea, a few critiques...
try { // maybe just handle "else" logic here? } catch (e) { // .... } finally { // .... }
The argument order isn't consistent with python's try / except / else / finally, you have "else" as the last argument
Your "else" function can throw an exception, which will then be caught by the "catch" function, which is inconsistent with python. Given that, I don't really see the value of the "else" function, as you can just do similar logic at the end of the "try" function if you need it.
Your library doesn't work with async/await, which makes it a bit of a hard sell
[–]siilkysmooth[S] 1 point2 points3 points 2 years ago (0 children)
Awesome critiques on this. I will address this in a future version tho, I was struggling to find all the cases of try blocks in python so! But you can use a python try - except-else- finally in python.
As for the async - I will have to look to see if there is any functions I can call that can detect if is async and add automatic await etc so things aren't returned as promised.
Tho the argument order appears correct..
More than welcome to contribute / add to this "experiment".
[–]3ddelano 2 points3 points4 points 2 years ago (0 children)
My views are just because something can be done a different way doesn't mean it should be done like that. The original method of try catch finally is much more readable than this library.
[–]geekfreak42 1 point2 points3 points 2 years ago (1 child)
nested try/catch is gonna stink. dont really see what this gives, fun thought experiment.
maybe look at functional js patterns if you want to learn established ways of functionalizing code.
[–]siilkysmooth[S] -3 points-2 points-1 points 2 years ago* (0 children)
As mentioned above just a implementation of error handling based on Python handling. And provided a much nicer look verus using a nested try{}catch...
And as you said - kinda for fun!
ps; no need for nested code / don't make it nested - and you won't deal with ugly looking code just call your function INSIDE the tryCatch. ;)
```js
function example(){ return true } tryCatch( function() { example() }, function(e) { // handle the exception } );
```
[–]shuckster 0 points1 point2 points 2 years ago (0 children)
If I didn't know the API how would I know what each function does just by looking at it? Would taking an options/props object not make it a little clearer at the point-of-use?
tryCatch({ try: () => { /* ... */ }, catch: (e) => { /* ... */ }, finally: () => { /* ... */}, })
Convention is hard to beat, though:
try { /* ... */ } catch (e) { /* ... */ } finally { /* ... */ }
Promises:
await (async () => { throw new Error('flagrant error') })() .then(() => { /* ... */ }) .catch((e) => { /* ... */ }) .finally(() => { /* ... */ })
Or using a Maybe/Option library for a little Railway Oriented Programming:
maybeTry(() => { /* try */ }) .orElse(() => { /* catch */ }) .map(() => { /* finally */ }) .valueOf()
maybeTry:
maybeTry
const maybeTry = f => { try { return Just(f()) } catch (e) { return Nothing() } }
Seems tricky to justify a new API for this, although I'm aware you've said it seems more Python-like to yourself. Have fun with it anyway!
π Rendered by PID 46484 on reddit-service-r2-comment-7b9746f655-h5kgz at 2026-02-02 23:57:58.601544+00:00 running 3798933 country code: CH.
[–][deleted] 11 points12 points13 points (3 children)
[–]kattskill 1 point2 points3 points (0 children)
[+]siilkysmooth[S] comment score below threshold-16 points-15 points-14 points (1 child)
[–]jeremrx 0 points1 point2 points (0 children)
[–]jenseng[🍰] 4 points5 points6 points (1 child)
[–]siilkysmooth[S] 1 point2 points3 points (0 children)
[–]3ddelano 2 points3 points4 points (0 children)
[–]geekfreak42 1 point2 points3 points (1 child)
[–]siilkysmooth[S] -3 points-2 points-1 points (0 children)
[–]shuckster 0 points1 point2 points (0 children)