0 day streak
javascript / beginner
Snippet

Basic Error Handling with Try...Catch

Use a 'try' block to wrap code that might cause an error. If an error occurs, the code inside the 'catch' block will execute instead of crashing the program.

snippet.js
javascript
1
2
3
4
5
try {
riskyAction();
} catch (error) {
console.log("An error was caught!");
}
nodejs
Breakdown
1
try {
Defines a block of code to be tested for errors while it is being executed.
2
riskyAction();
A placeholder for code that might fail or throw an error.
3
} catch (error) {
Defines a block of code to be executed if an error occurs in the try block.