How it Works, and Breaks

Let's look at the exceptions_division_calc script's arithmetic statements:

n = 20
d = 5

q = n / d


When n and d are integers, the script performs this calculation: q = n / d

So, if n is 20 and d is 5, then q = 20 / 5, and the answer is 4, and the script that solves the problem will report the correct answer.  

What happened when you changed d to 0?  Then, q = 20 / 0.  There is no answer because no number can be multiplied by 0 to result in 20.  Since this is a Python script, setting d to 0 will cause a type of exception called a ZeroDivisionError.  

Next, what happened when you modified the script to d = “Hello” ?  The string "Hello" isn’t even a number! This causes a type of exception called a TypeError.

The micro:bit stops executing the script’s statements at the point where the exception occurs.  So, when there is an exception in the program, the micro:bit never gets to the statement that prints the “Try again!” message.  But, the ZeroDivisionError and TypeError messages are helpful for debugging code.


Did You Know?

The exceptions that caused the micro:bit to stop mid-script can be "handled"  with Python's try, except, else, and finally statements.   Note that try and except go together, but else and finally are optional. Here is a template for handling code that could cause exceptions:

    try:
        # Statements here that might cause an exception.
    
    except:
        # If there was an exception, do something about it here.
    
    else:
        # If there was not an exception, do these statements
    
    finally:
        # Regardless of whether or not there was an exception,
        # do these statements.

Here a variation of except with statements that display the exception description and type.  The exception is stored in the variable e.  Printing print("Exception = ", e) shows “error = “ followed by the exception text.  et = type(e) stores the type of exception in a variable named et.  Printing the et variable with print("Exception type = ", et) displays the exception type in the terminal.  Recall that exception types include ZeroDivisionError and TypeError.

    except Exception as e:
        print("Exception = ", e)
        et = type(e)
        print("Exception type = ", et)

If you know that certain types of exceptions will occur, you can even add else statements that handle each type of exception differently, like this:

    except ZeroDivisionError:
        print("Can't divide by zero.")

    except TypeError:
        print("Expected a number.")

Here are some of the more common exception types/messages you might encounter:

TypeError When the item is the wrong type
ValueError When the item is the wrong type, as in it needs to be compatible with math operators
NameError When the item has not been defined
ZeroDivisionError When division by zero is attempted
IndexError When attempting to access an invalid index
KeyError When a key is not found
StopIteration When the next function goes past its limit