What are the differences between exceptions and if statements? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

What are the differences between exceptions and if statements?

In what cases do we need to apply exceptions instead of an if statement?

6th Jan 2021, 10:00 PM
eMBee
eMBee - avatar
3 Réponses
+ 1
Yes. That simple problem has a simple solution. My understanding of exception handling is that you are going to catch any error & then be able to handle it, might be a few different types of error get handled simply, other errors get a different treatment, & some need the program to save your data & terminate with an error message.
6th Jan 2021, 11:05 PM
HungryTradie
HungryTradie - avatar
+ 1
Jeff.C Can't we use a boolean expression and a comparison operator with an if statement to solve that problem?
6th Jan 2021, 11:01 PM
eMBee
eMBee - avatar
0
Python3: Exceptions are errors within a "try" statement. If you are monitoring for exceptions then your program can catch the error (and respond to it) instead of crashing your program. One use is to catch garbage inputs (eg: a string when you are expecting am integer) & then loop back to get the user to input a valid value.
6th Jan 2021, 10:55 PM
HungryTradie
HungryTradie - avatar