0

I have a function that wraps console.log like this.

NS.log("error");

which I use for error logging. It is a simple one liner to let the app developer know that something went wrong. It does not stop program flow and shows up unobtrusively in the console.

However, I've heard in university courses, etc., that using a try/throw/catch is best practice as it uses an error control system already in place.

However, I don't need the error to "bubble" up after a throw statement. I would catch it immediately like this:

try {
    throw "error"
} catch (e) {
}

Are there any cons to simply logging the error as opposed to using a more formal structure.

My assumption is that try/throw/catch is for more complex structures.

But I wanted to make sure.

Notes:

  • requirejs uses throw a total of 4 times | no use of console or log
6
  • 4
    try/throw/catch is not an error logging mechanism. It's a mechanism for allowing the code to handle exceptions. One way to handle them is to log them. Usually, an exception should change the flow of control of the application, because something is wrong. Commented Feb 27, 2013 at 19:57
  • Error handling is a very nuanced subject that has a lot of perspectives and a lot of forces (starting with the type of error that is being handled). My initial advice would be to stick to the simplest solution unless you have a reason to look elsewhere: so logging is fine. (Assuming this is browser JS and not server side) Commented Feb 27, 2013 at 20:03
  • @pure_code All code needs error handling. throw is only one method which IMHO isn't overly useful in JS and either way could be reserved for truly exceptional situations. You should always have code that does things like validation and returning proper expected error codes, particularly with the dynamism of JS. There are many forms of error handling. Commented Feb 27, 2013 at 20:10
  • One of the things to keep in mind is that JS normally runs in a hosted environment where infrastructural issues that could normally cause exceptions are far less likely to happen or be handled by the host. For a perspective on try/catch which could apply to JS check out: pragmatictips.com/34 Commented Feb 27, 2013 at 20:14
  • input validation would be a terrible place to use try/catch. Entirely localized errors have no need for a throwing. Commented Feb 27, 2013 at 20:27

2 Answers 2

1

If it works for you and those your working with then it works; and there is no need to change.

Technically try/throw/catch is best practice, and in most languages that's what you should use.

In javascript however, I can tell you that try/throw/catch has pretty bad performance. Also a lot of errors in javascript are of a dynamic nature. The should be dealt with yes. But a lot of them won't break your application.

So why would you want them to bubble up and annoy your users?

TL:DR I'm not discouraging the use of try/throw/catch sometimes you should use it, as @JohnSaunders said. But the fact that often in javascript errors don't break anything and you often take a performance hit using try/throw/catch, means you should think about the cost/benefit ratio of this 'best practice' before mindlessly implementing it.

1
  • @pure_code The point of a try catch is to deal with issues not just log them. If there is something gives an error its not so much that the error annoys users (though it certainly will if they can't do what your app says it should do), its that if you use a try catch then you should be doing something to change the flow of work. It's pointless to use a try catch if all your going to do is print the error to the console. The console is on their side, what are you going to do with that?
    – Ryan
    Commented Apr 26, 2013 at 16:07
0

I am happy you are considering your options and thinking through the process. However

  1. The try statement lets you to test a block of code for errors.
  2. The catch statement lets you handle the error.
  3. The throw statement lets you create custom errors.

so in your case if you have some kind of log process in place then use try catch. example

try {

} catch (e) {
   NS.log(err);
}

more information /source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.