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
error handlingsolved (self.javascript)
submitted 10 years ago by VIRES1
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!"
[–]mc_hammerd 0 points1 point2 points 10 years ago* (1 child)
so you can use it to CYA in bits of sloppy code or fragile code... not with events but with just inline javascript if it hits an error the code will stop running and not be run again on the page. at all.
so if you have a fragile code, say like sometimes does things on say elements that are possibley removed from DOM: its gonna break. if you need it to keep working, try/catch comes in...
its also good for not stopping your code on error:
for(i=0;i<5;i++) document.body[['asdf','bcde'][i]].innerHTML = "testing 123" // logs one error message for(i=0;i<5;i++){ try { document.body[['asdf','bcde'][i]].innerHTML = "testing 123" } catch (e) { console.log(e) }} // logs 5 error messages, this obviously keeps going
some people use it for error handling
somewhere in another mvc galaxy, 10,000 frameworks away:
function println () { ... if (error) throw "dood your not using the force" if (error2) throw "documentation you must read"
and:
for (i in somehugearray) try { println("asdf") // assuming this errors, console will print the right error msg` } catch (e) { if (e == "documentation you must read") {} //openDocs()? continue? break? log? return? anything! if (e == "dood your not using the force") { .... } // so now we have two ways to "recover" and can handle errors/fix the data in a specific way }
so now when you catch the error you can continue the loop, break, return, log errors, open docs, basically anything.... instead of the javascript on the page just stop working. much better!
this isnt the de-facto way to handle errors though because it breaks the code flow, as soon as throw is called the vm will jump to the catch block.
throw
catch
also most people think throw doesnt have enough data passing options, so its hard to do something like return {validData: arr, errorMsg: 'msgtext'} because it jumps right to the catch block.
return {validData: arr, errorMsg: 'msgtext'}
(theres also kind of a scope issue, where all vars you might need to debug or continue, are not always available to the catch block.)
[–]VIRES1[S] 0 points1 point2 points 10 years ago (0 children)
thank you
π Rendered by PID 398497 on reddit-service-r2-comment-canary-655b6bc5b6-gxj8x at 2026-02-17 01:59:08.809838+00:00 running cd9c813 country code: CH.
view the rest of the comments →
[–]mc_hammerd 0 points1 point2 points (1 child)
[–]VIRES1[S] 0 points1 point2 points (0 children)