Exception Handling Assignment 1
Exception Handling Assignment 1
try:
# ...
except ExceptionType:
# ...
By using this try-except structure, we can gracefully handle
exceptions and prevent our program from crashing unexpectedly.
ValueError TypeError
OverflowError IndexError
KeyError
ZeroDivisionError
ImportError FileNotFound
Error
4. What is an assertion in Python? (1 mark)
For example, let's say you're writing a program that calculates the
average of a list of numbers. Before performing the calculation, you
can use an assertion to check if the list is not empty. If the list is
empty, the assertion will raise an AssertionError, indicating that
something unexpected has occurred.
5. What is the difference between assertion and exception handling
in Python? Use the Venn diagram below to answer. (4 marks)
Assertion Exception