4.2 Error Types

So far in Python, you may have come across statements like “TypeError…”, “SyntaxError…”, or “ZeroDivisionError…” - these are all error types (or exceptions) that are built into Python’s standard library.

While prof. Marek probably won’t expect you to know about exceptions in too great of a detail, you can consider checking out this link if you want to know more about the different types of exceptions in Python: https://www.tutorialsteacher.com/python/error-types-in-python.

4.2.1 So… why even bring this up in the first place?

Remember how I talked about the try, except, and the finally keywords in the previous section? While you can use except like shown in the previous examples, what happens if your code in the try block returns more than one kind of error?

In a sense, I was very lucky in that the example I coded out only gave me one kind of exception: a ValueError (this happens when you do not give an appropriate value to a function). But, if I had code that also yielded a ZeroDivisionError (pretty self-explanatory - what you get if you attempt to divide by zero in Python), I could do this instead:

# Code here...

try:
  # Faulty code that raises an exception
except ZeroDivisionError:
  # Handles ZeroDivisonErrors
except ValueError:
  # Handles ValueErrors
finally:
  # ALWAYS run the code here (optional)

And Python would handle the errors as such!