PayamB.
PayamB.

Reputation: 754

node js multiple try catches

is it a bad practice to use multiple try/catches next to each other ? how do they work and execute in nodejs and what is the better practice ? one try catch for the whole piece of code or multiple try catches ?

const function = () => {

try{

   // some code 

   } catch(e) {

     console.log(e);

}

try{

   // some other code 

   } catch(e) {

     console.log(e);

   }

} 

vs

const function = () => {

try{

   // all of the code 

   } catch(e) {

   console.log(e);
}

} 

Upvotes: 2

Views: 4848

Answers (4)

Andreas
Andreas

Reputation: 444

It actually depends on your code logic. Supposing you follow a synchronous pattern (async-await), multiple try catch can be a bit tricky. For example, if a variable in your third try-catch block depends on a variable that has failed to get a value (or gets a wrong value) in your first try-catch, you have to handle it separately. So in a multiple try catch, i would first make sure that the parts of the code that depend on each other, can provide the right outputs. This can be quite tiresome. Personally, i prefer using a single try-catch in most cases, using something like this:

try {
  await doSomething();
  await doSomethingElse();
} catch(err) {
  return({"error": err.message});
}

Upvotes: 0

Sohan
Sohan

Reputation: 6809

I would recommend using multiple try/catch when you need to deal with different error response and you care about processing their errors

Having single try/catch block for one piece of function that does not care about actual error and just needs to process generic error. This is the common and widely used approach.This also make your code look clean and neat

I follow build common error and throwing back the actual error in context by building that error:

For eg:

    try
    { 
    const response =  await updateCollection(data);
    await processResponse();
    }        
    catch(e) 
    {
   // This build common error with actual error context (wrapper)
   let buildError = buildMyErrorWithContext(e)
   return buildError ;
    }

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 370729

It can be a useful technique if you need to be able to differentiate between different sorts of errors that can be thrown on different lines. For example:

try {
  await verifyCredentials();
} catch(e) {
  // (<send response to user that they aren't authenticated>)
  return;
}
try {
  await saveToDatabase();
} catch(e) {
  // (<send response to user that there was a problem saving something to the database>)
  return;
}

That's just fine.

In contrast, if you just want to be able to see if an error happens, but you don't care about differentiating between different types of errors, a single try block will be terser and easier:

try {
  await verifyCredentials();
  await saveToDatabase();
} catch(e) {
  // (<send response to user that there was a problem>)
  return;
}

Both options have their place.

Upvotes: 4

Smit Vora
Smit Vora

Reputation: 470

It's depend on your project requirement, if you don't want to execute code after first error then you can use multiple try catch,else single try catch can work.

Upvotes: 0

Related Questions