Reputation: 60975
I'm reading about error handling in nodejs, and I came across something unsettling while reading this document:
http://nodejs.org/api/domain.html
It says "By the very nature of how throw works in JavaScript, there is almost never any way to safely "pick up where you left off", without leaking references, or creating some other sort of undefined brittle state."
This sounds horribly horrible. Is this really saying that any time an exception is thrown, I need to shut down the thread? I feel like I'm missing something here.
Upvotes: 2
Views: 936
Reputation: 707976
There's nothing wrong with throwing exceptions in the right circumstances. It is a useful tool and can be used as such. Exceptions are generally not the right tool for normal, expected, frequently used code paths because they are slow, much slower than normal return values. It's usually better to use return values for those types of circumstances if performance matters to you.
But, exceptions can significantly simplify your code for unexpected error conditions or non-normal conditions and, in a memory managed language like javascript, you generally don't have to worry about memory leaks when throwing an exception except if you are in the middle of manipulating persistent global state when you throw the exception. All local variables and their references are cleaned up for you when an exception is thrown when they go out of scope.
Exceptions don't lead to memory leaks or a brittle state unless your code is poorly written which is the same for any other method of indicating an error condition.
Upvotes: 3