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, 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

 


Printer-friendly version
Make a Script Handle Exceptions
Prev
Try This: Add Exception Handler
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2026 Parallax Learn • Built with GeneratePress