Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses

Exception Handling Primer

Curriculum

  • 1 Section
  • 9 Lessons
  • Lifetime
Expand all sectionsCollapse all sections
  • Exception Handling Primer
    9
    • 1.0
      Exceptions and Exception Handling
    • 1.1
      Make a Script Handle Exceptions
    • 1.2
      How it Works, and Breaks
    • 1.3
      Try This: Add Exception Handler
    • 1.4
      Your Turn: Coder-Friendly Hints
    • 1.5
      Make an App that Won’t Freeze
    • 1.6
      How it Works: ValueError
    • 1.7
      Try This: Add Exception Handler for User Errors
    • 1.8
      Your Turn: User-Friendly Hints

Try This: Add Exception Handler for User Errors

The first step to preventing a user from causing an exception is to test as many user mistakes as you can think of.  Make sure to record each type of exception that occurred.  You will need that list later so that each exception type can be handled properly.  

To speed up the process, use the except Exception as e block, and print the exception’s text and type.  This will allow you to keep the app running as you test various user errors that cause exceptions.  Here is an example.

Example script: exceptions_user_division_calc_try_this

  • Change project’s name from  exceptions_user_division_calc to exceptions_user_division_calc_try_this.
  • Add try…except…else…finally statements as shown here.
  • Remember to add indentation to the code blocks that are nested in try:, except:, else:, and finally:.
  • Save the modified script.
  • Click the Send to micro:bit button.
# exceptions_user_division_calc_try_this.py

from microbit import *

sleep(1000)

print("Division Calculations")

while True:
    try:
        text = input("Enter numerator n: ")
        n = float(text)
    
        text = input("Enter denominator d: ")
        d = float(text)

        q = n / d

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

    else:
        print("Quotient q: ")
        print("q = n / d")
        print("  = ", n, " / ", d)
        print("  = ", q)

    finally:
        print("Try again!")
        print()
  • In the serial monitor, click to the right of the Enter numerator n: prompt.
  • Repeat the user mistakes you tried earlier, and make notes of what happened when you entered 0 and then “Hello” for the denominator.  
  • Make notes of the error types that were reported for each “mistake.”
  • Can you make any other “mistakes” that cause exceptions? If so, record the error types you encounter.

 


Printer-friendly version
How it Works: ValueError
Prev
Your Turn: User-Friendly Hints
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2026 Parallax Learn • Built with GeneratePress