Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it good to use try catch in nodejs

i just want to know is it good to use try catch in nodejs. for example i am using crypto modules of node js, for encryption & decryption. so i am handling any error while encryption/ decryption in try catch is it a good practice ?????

like image 964
pitu Avatar asked Feb 16 '16 09:02

pitu


People also ask

What is try catch in Node JS?

Node.js Try Catch. Node.js Try Catch is an Error Handling mechanism. When a piece of code is expected to throw an error and is surrounded with try, any exceptions thrown in the piece of code could be addressed in catch block. If the error is not handled in any way, the program terminates abruptly, which is not a good thing to happen.

Why do we need to use try- catch?

I think the accepted answer is generally true, but there are good reasons to use try-catch, and even throw, other than when dealing with native objects. When an error is thrown rather than passed to a callback, it halts further execution and jumps up the stack to the first block than can handle it.

Is there a way to avoid try catch in JavaScript?

There is one other way to avoid try catch using maybe and Either (monads) we had a web scrapper written in node. We were able to remove all of try catch using it. Show activity on this post. Try/catch in Javascript is not as bullet-proof as in other languages, due to Javascript's asynchronous nature.

Should I use try/catch for encryption/decryption?

If your encryption/decryption calls are synchronous (depends on the library, module, function you use), try/catch is ok to use it, otherwise depending on how you've used it, it might be useless. Try/catch is ok to catch a programming error or buggy code, for example from libraries you don't know might be buggy.


Video Answer


1 Answers

If your encryption/decryption calls are synchronous (depends on the library, module, function you use), try/catch is ok to use it, otherwise depending on how you've used it, it might be useless.

Try/catch is ok to catch a programming error or buggy code, for example from libraries you don't know might be buggy.

Like:

try {
  libraryLib.encrypt(notSureIfThisIsAValidParam);
} catch (err) {...}

You're not sure if the user/password are set, and you're not sure if the crypto library might fail, so you wrap it.

But try/catch also creates a new execution context, copies over scopes and all, and this is a costly operation, in terms of CPU time. It means that you should try to avoid try/catch in hot code paths.

Logic/business errors in Node are usually handled with error-first callback patterns (or Promises, or similar). Generally you'd only use try/catch if you expect programmer errors (ie. bugs), and other patterns for errors you expect.

Of course, this is just one line of reasoning, and I generally see a lot of intentional throw clauses (meant to use with try/catc) in Node code written by Java converts. But that might be specific to me, so YMMV.

like image 152
Zlatko Avatar answered Sep 28 '22 14:09

Zlatko