Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Third party JavaScript - good idea to use try catch?

Tags:

javascript

I am developing a third party JavaScript widget that will be included by users on their applications/blogs. I have good tests around the library, but I am afraid that if despite that if some syntax error sneaks through and cause the other scripts on the user's application to stop loading.

So - to prevent this from happening, is it a good idea to surround my entire widget code in a try/catch like this?

try {
    // my library
} catch(e) {
    // notify me about the error
}
like image 399
jeffreyveon Avatar asked Dec 13 '11 15:12

jeffreyveon


People also ask

Should you use try catch in JavaScript?

The try-catch statement should be used any time you want to hide errors from the user, or any time you want to produce custom errors for your users' benefit. If you haven't figured it out yet, when you execute a try-catch statement, the browser's usual error handling mechanism will be disabled.

When should you not use a try catch block?

Try-catch is best used in parts of your code when you think mistakes will occur that are out of your control for whatever reason. 2. When Should You Avoid Using Try-Catch? If you know an error is likely to happen, you shouldn't use the try-catch statement since you'd rather debug the problem than disguise it.

Should you use try catch in production?

Try-Catch-Finally blocks are perfectly acceptable (and really almost mandatory) in production code, as they offer a means of falling back in exception enabled languages. You'd be hard pressed to find a piece of production java code that doesn't have any try-catch blocks of some sort.

Does try catch stop execution JavaScript?

The “try… First, the code in try {...} is executed. If there were no errors, then catch (err) is ignored: the execution reaches the end of try and goes on, skipping catch . If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err) .


1 Answers

Here is a good common approach to what try-catch blocks are used for. If you can catch an exception and do something with that exception then go ahead and catch it. For example, BadHtmlException or something similar is an exception you can catch to provide user with feedback that you should fix the HTML and try again.

There are types of exceptions that there is no action that can be done. For example, the application was configured incorrectly with a bad user/password. This should be a critical error and should be push all the way up to the application. Possibly an exception that might not make sense to the user.

So what am I suggesting? I am suggesting don't wrap anything in a try-catch unless you know there will be that exception thrown. If there is a bug or exception, the person using your code should see it and report it as an issue. You really can't spend all your time going through possible issues that may or may not be your code.

Finally, you should write unit tests and make sure each part of your library is well tested before each release. Doing this will make sure that future releases don't break anything.

like image 133
Amir Raminfar Avatar answered Sep 24 '22 03:09

Amir Raminfar