Category: javascript
Errors handling using try and catch
Published on 16 Jul 2026
Explanation
Errors can be handled using try...catch blocks.
Code:
try {
console.log(a);
} catch(err){
console.log(err);
}
Explanation
The finally block executes regardless of success or failure.
Code:
try{}catch{}finally{
console.log('Done');
}
Explanation
throw creates custom errors.
Code:
throw new Error('Invalid Input');
Explanation
Error objects contain useful debugging information.
Code:
console.log(err.message);
Explanation
Proper error handling prevents application crashes.
Code:
if(!data) throw new Error('No Data');