I suppose calling 3 javascript (jquery) files like this:
<script type="text/javascript" src="js/file1.js"></script> <script type="text/javascript" src="js/file2.js"></script> <script type="text/javascript" src="js/file3.js"></script>
On file2.js there are script or function goes wrong, for example, the called function does not exist. The consequences because error in file2.js are scripts in file3.js will not be executed.
Whether there is a way to script in file3.js still executed? Possible to bypass or ignore the error on file2.js?
Thank you.
JavaScript provides error-handling mechanism to catch runtime errors using try-catch-finally block, similar to other languages like Java or C#. try: wrap suspicious code that may throw an error in try block. catch: write code to do something in catch block when an error occurs.
In JavaScript, all exceptions are simply objects. While the majority of exceptions are implementations of the global Error class, any old object can be thrown. With this in mind, there are two ways to throw an exception: directly via an Error object, and through a custom object.
It's best to avoid throwing errors from inside a Promise, because they may not always be caught, depending on how the code that called them is structured. However it's good practice to return an error when rejecting a Promise, and you can return Error custom types just like any other Error.
You can use a try catch block around the code with the error
try{ //code that causes an error }catch(e){ functionToHandleError(e); } //continue from here
But you should only do that if the error is a known condition/unavoidable problem with an external library or only happens on bad user input. You shouldn't use it to allow the program to keep running at all costs.
Some ways to avoid errors
var
keyword, or are attached to a global object, they can be modified in places other than they are declared. Javascript also doesn't have block scope. Make sure you know all the places your variables are being modified, and try to limit the scope of your variables when possible. Especially avoid using too many global variables to hold state, as it can be difficult to know the current state without many null/type checks.Some ways to handle errors
If its an expected problem (IE if the input may be empty and requires different handling), ideally check it with an if statement beforehand. If you don't want to do that, you can use try/catch to cast the input, ask for new input, or act on the input given as approperiate.
if its an unexpected problem (IE you don't know whats going on) a catch block can be a good place to output diagnostic information during development. IE, don't just catch it and let it fail silently, output a message to let you or other developers know where it failed, and then use your favorite browser's web developer tools to inspect the values at the time, figure out what happened, and add handling for that case to fix it.
What not to do
put the try/catch in, and then just let the program keep on running. If the code that isn't running correctly is unnecessary, get rid of it. If it is necessary and isn't working correctly, fix it.
use try catches as your input validation (IE if the function is null it will throw an exception and I'll catch it, then I know the function is null). Whenever possible, check for any expected errors. Try/catches should handle "exceptions" when the behavior is unusual and you need to go down an alternative code path or alert the user that something is wrong.
When you don't have time to debug try this:
function stoperror() { return true; }
Call it:
window.onerror = stoperror;
The functionality is very simple: create a function that always returns true, and then whenever an error occurs, call this function (returning true and suppressing the error).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With