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

How it Works: ValueError

This type of application is explained in detail in Computer – micro:bit Talk, leading up to the apps in the Input/Print for Applications page.  So, let’s just look at the exceptions that occurred.  

Like the previous activity, when you typed 0 and pressed enter, a ZeroDivisionError exception occurred at the q = n / d statement since it cannot give you an answer with d = 0.  No surprise here.

Next, when you tried “Hello” in the denominator,  a new error occurred—the ValueError. This is different from TypeError we saw previously. In our earlier example that used the terminal, d was defined as an int. Here, d is defined as a float.

So, here the code froze when it tried to execute float(“Hello”).  The float function can convert text that represents a value into that value.  So, in d = float(“5”), the float function receives the text string “5” and returns the value 5.  That value gets stored in the variable d.

If a variable named text stores the string “5”, then d = float(text) will also store the value 5 in d.  In contrast, d = float(“Hello!”) has non-digits that the float function cannot convert to a number.  At that point, the MicroPython runtime raises the ValueError exception.


Did You Know?

While developing the app, it’s still a good idea to to display the error and type so that you can attempt to “break” the code with different variations of user input errors.

Your script can selectively address more than one kind of exception with a single action.  For example, if you want it to display the “Expected a number.” message for either a TypeError or a ValueError, you could use this:

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

 


Printer-friendly version
Make an App that Won’t Freeze
Prev
Try This: Add Exception Handler for User Errors
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2026 Parallax Learn • Built with GeneratePress