JavaScript Error handling in 9 minutes! ⚠
YouTube Viewers YouTube Viewers
1.87M subscribers
7,982 views
0

 Published On Nov 30, 2023

#JavaScript #tutorial #course

00:00:00 introduction
00:00:18 Errors
00:01:36 try/catch
00:02:38 console.error()
00:02:58 finally
00:04:16 throw new Error()
00:08:28 conclusion

// Error = An Object that is created to represent a problem that occurs
// Occur often with user input or establishing a connection

// try { } = Encloses code that might potentially cause an error
// catch { } = Catch and handle any thrown Errors from try { }
// finally { } = (optional) Always executes. Used mostly for clean up
// ex. close files, close connections, release resources

try{
const dividend = Number(window.prompt("Enter a dividend: "));
const divisor = Number(window.prompt("Enter a divisor: "));

if(divisor == 0){
throw new Error("You can't divide by zero!");
}
if(isNaN(dividend) || isNaN(divisor)){
throw new Error("Values must be a number");
}

const result = dividend / divisor;
console.log(result);
}
catch(error){
console.error(error);
}
finally{
console.log("This always executes");
}

console.log("You have reached the end!");

show more

Share/Embed