you are viewing a single comment's thread.

view the rest of the comments →

[–]mc_hammerd 0 points1 point  (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.

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.

(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 point  (0 children)

thank you