Try and Catch statement example.
You will need at least some knowledge of JavaScript if you want to use this example.
Example
Step by step guide
You will need at least some knowledge of JavaScript if you want to use this example.
Example
Code:
try {
//Try statement lets you test your code for errors
console.log('Hello');
} catch (err) {
//Catch statement "catches" any errors from your try statement and executes whatever you've placed as your error handling
console.error('error occurred : ' + err);
}
Step by step guide
- Create your try statement:
Code:
try {
}
- Add your code inside the try statement:
Code:
try {
console.log('Hello');
}
- Add a catch statement to the end of your try statement and make a function called err
Code:
catch (err) {
console.error('error occurred : ' + err);
}
- Now run your code and if it catches any errors it will console.error() them.