Home >> Tutorials >> Node.js try catch - Error Handling

Node js Try Catch

JavaScript's try-catch-finally statement works very similarly to the try-catch-finally in C++ and Java.

try {
    throw new Error();
} catch(e) {
    console.log(e); // [Error]
}

e in this example is the exception value.

#Example:

try {
  throw "thrown error message";
  console.log("this message will never be shown!");
}
catch (e) {
  console.log(e); //Catch error message thrown
}
finally {
  console.log("it's finally block"); // It's always get executed
}

Output:

thrown error message

it's finally block

Full Stack Tutorials

Author @FullStackTutorials | View all Articles