简体   繁体   中英

Javascript Catch, but Still Throw Error

I've built a javascript function that merges multiple ajax functions into a single request. With each request, there is almost always a callback function. The bundled function calls the callback functions one at a time.

I've surrounded these with a try catch to prevent one function from preventing the other functions from running. However, I would still like the browser to notify me about javascript errors. How can I do this?

I've set an example of what I'm trying to do up here .

The code from the jsfiddle link above:

<script>
function doSomeStuff() {
    for (var i in arguments) {
        try { 
            arguments[i].apply(this);
        }
        catch (e) {
            throw e;
        }
    }
}

doSomeStuff(function() { console.log("i'm an apple"); }, 
    function() { imnotavariable = test; },
    function() { console.log("i'm an apple too"); });
</script>

Summary of Question: How do I catch an error, while still reporting it to the browser?

If you want the loop to finish (ie call all your functions), you can't throw the error within it. One approach of achieving what you want is to save the error and throw it after the loop, ie

var error = null;
for (...) {
  try {
    ...
  }
  catch(e) {
    error = e;
  }
}
if(error != null) { throw error; }

This will only propagate the last error though. If you want to catch all of the errors that occur, you would need to merge them into a single one that you can then again throw.

Alternative solution by Tom

You can also set a timeout on the throw so that it doesn't interfere with the execution of the functions.

function doSomeStuff() {
  for (var i in arguments) {
    try { 
      arguments[i].apply(this);
    }
    catch (e) {
      setTimeout(function() { throw e; }, 100);
    }
  }
}

You need to define what you mean by "still reporting it to the browser".

The short answer might be that if you had some alternate logging system you could try doing something similar to this:

    var apperrors;

    function appLog(error){
        apperrors = apperrors || [];

        apperrors.push(error);
       //maybe trigger an app error event?
    }

    function doSomeStuff() {
        for (var i in arguments) {
            try { 
                arguments[i].apply(this);
            }
            catch (e) {
                appLog(e);
            }
        }
    }

And then do something with your errors. You don't really describe what that something is though.

EDIT

Following the comment below: if you catch the error you have three courses of action I can see:

  • ignore it (That's bad)
  • rethrow it and do something additional (ie because you want to notify the user more gracefully)
  • or catch the error, and handle it in some other fashion that isn't as agressive

I you re-throw the error you're not gaining anything unless you do something 'additional'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM