There are more exceptions than just ZeroDivisionError and ValueError. There are several exceptions to choose from but I would like to discuss three that are useful to know about. These are AttributeError, TypeError, and of course the dreaded SyntaxError. TypeError can be induced when your code receives an invalid value for its argument. Here’s an example.
Ex:
var1 = 5
var2 = “five”
var3 = var1 + var2
This code will return the TypeError message because you cannot add a string and an integer together. Another piece of code that will return TypeError is print(list[0.5]). Assume in this case the list variable is an existing list. 0.5 is not a valid indice it will not accept that data type, a float.
Next up there’s the attribute error. Here is an example.
Ex:
list = [1, 2, 3, 4, 5]
list.append(6)
list.depend(7)
This code will return an AttributeError because depend() is not a valid method for lists. As much as you might want to you cannot just make up random methods and expect them to work, that is what functions are for.
Then of course there’s the SyntaxError this is likely to be your most common error you’ll see this will be any piece of code that doesn’t fit Python’s grammar. In other words if you type bhsdjofbdsh oagf into the terminal and press enter you’re going to get a SyntaxError.
Now in terms of testing your code, the best way to test is to identify the executions paths. Essentially look over your code and see what are all the ways this could execute. If you have code that asks the user to input a positive number, negative number, or zero. You should attempt to enter 0, a negative number, and a positive number in order to verify that the code runs successfully.