Try/catch is an error handling mechanism in JavaScript that helps us to manage exceptions (i.e., errors) that may occur during the execution of the program. This helps to maintain normal flow of program even after an error occurs.
Here is the common structure:
```
try {
// code that might throw an exception
} catch (error) {
// code to run if an error occurs
}
```
`try` statement allows us to define a block of code to be tested for errors while it is being executed.
`catch` statement allows us to define a block of code to be executed, if an error occurs in the try block.
Here is an example:
```
try {
var a = “abc”;
a = a.filter(n => n); // Here error will be raised because filter is not a string method.
} catch (error) {
console.log(error.name); // Error name: TypeError
console.log(error.message); // Error message: a.filter is not a function
}
```
The error object inside the catch block has two main properties:
- error.name: This holds the name of the error (E.g., `TypeError`, `SyntaxError`, `ReferenceError`).
- error.message: This holds the textual message that explains the error.
In critical code pieces where error is likely to happen, try and catch can be effectively used to gracefully handle errors and ensure that the rest of the code executes perfectly. There is also a `finally` block that can be added which will be executed regardless of an error being thrown or not.