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
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.throw
is only one method which IMHO isn't overly useful in JS and either way could be reserved for trulyexception
al 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.