Is something like this enough?
var _errors = [];
function flushErrors() {
if ( _errors.length > 0 ) {
e = _errors;
errors = [];
throw e;
}
}
function pushError( e ) {
if ( console && console.error )
console.error( e );
//if ( ... )
// ...
else
_errors.push( e );
}
function runHandlers(){
var h;
try {
while ( (h = handlers.shift()) ) {
h();
}
} catch (e) {
pushError(e);
if ( h )
runHandlers();
}
flushErrors();
}
var handlers = [
function(){
throw new Error("A");
},
function(){
throw new Error("B");
}
];
On Mar 25, 5:39 am, John Resig <[email protected]> wrote:
> 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.
>
> Additionally, using setTimeout is no good because the return results
> from the functions need to be handled synchronously.
>
> --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
-~----------~----~----~----~------~----~------~--~---