JavaScript Error handling in 9 minutes! ⚠

preview_player
Показать описание
#JavaScript #tutorial #course

00:00:00 introduction
00:00:18 Errors
00:01:36 try/catch
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{

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;
}
catch(error){
}
finally{
}

Рекомендации по теме
Комментарии
Автор

// 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!");

BroCodez
Автор

repetition is the mother of learning, fellow programmers!

xmorlanzz
Автор

I love these short videos....it's good for revision

justmakeitviral
Автор

i always did lots of if statements to handle errors and i thought it was the best way lol

hunin
Автор

the idea of the content is as impressive as ever ❤

nut_anime
Автор

Thank you for this tutorial video. I really helped my understanding

Sijo
Автор

But why not just check if result is Infinity or NaN after we performed the division? Actually, the fact that JS doesn't fail on division by zero is an intentional feature of JS.

dimitro.cardellini