JavaScript Internals (521 Qs)•Part 4: Prototypes, Object Methods & Event Loop (174 - 253)
What are the various statements in error handling
Below are the list of statements used in an error handling,
- try: This statement is used to test a block of code for errors
- catch: This statement is used to handle the error
- throw: This statement is used to create custom errors.
- finally: This statement is used to execute code after try and catch regardless of the result.
Practical Code Example
javascript
// Error Handling Statements:
try {
throw new Error('Something went wrong!'); // throw statement
} catch (err) {
console.error('Caught:', err.message); // catch statement
} finally {
console.log('Cleanup always executes'); // finally statement
}