javascript / beginner
Snippet
Basic Error Handling with Try...Catch
The try...catch statement marks a block of statements to try and specifies a response should an exception be thrown. This prevents the Node.js process from crashing.
snippet.js
1
2
3
4
5
try {const result = 10 / undefinedVariable;} catch (error) {console.log('Caught an error:', error.message);}
nodejs
Breakdown
1
try {
The block where you run code that might throw an error.
2
} catch (error) {
The block that executes if an error occurs in the try block.