Now that you know what exception types to expect, you can modify your application to handle each one by responding with helpful user prompts. Your customers will thank you one day!
- Change project's name from exceptions_user_division_calc_try_this to exceptions_user_division_calc_your_turn.
- Change the except block from this:
except Exception as e: print("Exception = ", e) et = type(e) print("Exception type = ", et)
...to this:
except (TypeError, ValueError): print("Expected a number.") except ZeroDivisionError: print("Can't divide by zero.")
- Save the modified script.
- Click the Send to micro:bit button.
- In the serial monitor, click to the left of the Enter numerator n: prompt.
- Test the responses to each exception you can create, including entering zero for the denominator, typing “Hello” instead of a number, and so on.
- Now imagine you are using this app without knowing how to write code. Isn't this an improvement?