Thanks John to clarify me what I missed. > The problem with your code is that you're re-throwing the event - > which means that debuggers/command-lines will think that the error > originated from the second throw point in the catch, and not the > original location.
I was in hope that there is an good JS debugger that will report entire stack (Firebug sometime have problems with doing that - in that cases, instead of re-throwing, console.error works better to me). I can understand if this is not enough for many users, so I have to agree with you. > Additionally, using setTimeout is no good because the return results > from the functions need to be handled synchronously. Not sure why it would not be possible to "synchronize" the return results. I guess that it would be more difficult to implement, so I will not insist about this. I have to worry if you will implement something that will not throw ALL errors, because it would hide some errors if an required third- part plug-in already throws some. If it is too hard to ensure all throws, than I certainly prefer the current solution. > --John > > On Wed, Mar 25, 2009 at 12:32 AM, Robert Katić <[email protected]> wrote: > > > What about this: > > > function runHandlers(){ > > var h; > > try { > > while ( (h = handlers.shift()) ) { > > h(); > > } > > } catch (e) { > > setTimeout(runHandlers); > > throw e; > > } > > } > > > var handlers = [ > > function(){ > > throw "A"; > > }, > > function(){ > > throw "B"; > > } > > ]; > > > This one will throw both (all) errors. > > > Am I missing something? > > > On Mar 24, 10:13 pm, John Resig <[email protected]> wrote: > >> > I don't think it is the resposibility of the dispatcher to handle > >> > exceptions. > >> > I think it it is the resposibility of the dispatcher to dispatch > >> > events. :-) > >> > An error in one handler should not prevent another handler from > >> > executing. > >> > Nor should a dispatcher suppress errors so that it can complete its > >> > task. > > >> Why not just use try/finally, then? > >> Online here, as well:http://ejohn.org/files/handler.html > > >> <script> > >> function runHandlers(i){ > >> i = i || 0; > >> try { > >> while ( i < handlers.length ) { > >> handlers[i](); > >> i++; > >> } > >> } finally { > >> if ( i < handlers.length ) { > >> runHandlers( i + 1 ); > >> } > >> } > > >> } > > >> var handlers = [ > >> function(){ > >> document.write("testA<br>"); > >> throw "A"; > >> }, > >> function(){ > >> document.write("testB<br>"); > >> } > >> ]; > > >> try { > >> runHandlers();} catch(e){ > > >> document.write("ERROR: " + e + "<br>");} > > >> </script> > > >> Output: > >> testA > >> testB > >> ERROR: A > > >> I don't think the order of the error is terribly important in this > >> case. At the very least, though, you get the best of all worlds: All > >> handlers execute, exceptions are thrown, and performance isn't > >> sacrificed. > > >> --John > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jQuery Development" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~----------~----~----~----~------~----~------~--~---
