Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I skip or ignore errors in javascript / jquery?

Tags:

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.

like image 432
Fredy Avatar asked Apr 19 '13 03:04

Fredy


People also ask

How do you handle errors in JavaScript?

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.

Are there exceptions in JavaScript?

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.

Should you throw errors in JavaScript?

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.


2 Answers

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

  • check that functions exist before calling them
  • check that an object exists before referencing its properties
  • if user input is involved, validate the input to check that it's the expected type/range/etc
  • if this is a loading order problem consider reordering your scripts, or if this is a larger application using a dependency management system like requireJS
  • if objects are undefined after an asynchronous request, remember that code does not wait on an async request and it will not be defined until afterwards. Code dealing with the provided values should be done in a callback function or using deffereds/promises.
  • if you're referencing dom elements, consider starting your JS code from within a function that runs when the DOM is loaded such as jQuery's document.ready or the native window.onload.
  • if javascript variables are declared without the 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.

like image 183
Ben McCormick Avatar answered Sep 24 '22 01:09

Ben McCormick


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).

like image 30
Jamil Bashir Avatar answered Sep 26 '22 01:09

Jamil Bashir