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

Exceptions and Exception Handling

An “exception to the rule” is a special case or situation where the rule doesn’t apply.  With this kind of exception, someone usually points it out, and people normally talk together and figure out the best way to handle the exception.

When your micro:bit’s runtime engine encounters an exception to the rules created by your script, it is also called an exception.  But your script can’t point it out to you unless you equip it to do that. Statements can be added to scripts telling the micro:bit what to do in response to exceptions.  They are often referred to as exception handling statements, or sometimes just exception handling.  

When an exception is unhandled, it means there are no statements that catch and correct the problem.  In this case, the micro:bit runtime stops executing statements and displays an error message in the LED display with a description and line number.  The most important thing about adding exception handling statements is that they can keep your script running after the exception has occurred, and even fix the problem.

Exception handling code has many variations, but the main ingredients are try and except.

try:
     # Statements that might cause an exception
except:
    # Statements that “handle” the exception so that
    # the micro:bit can keep running its script.

 
A more complete exception handler might look like this

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

The next example will demonstrate how to test and handle exceptions caused by simple programming errors.  The example after that is especially important because it demonstrates how exceptions caused by user errors (like typing mistakes) can be handled.  With that kind of error handling, you won’t have to restart your micro:bit because it will handle the error by displaying a helpful message and prompting you to try again.

 


Printer-friendly version
Make a Script Handle Exceptions
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2026 Parallax Learn • Built with GeneratePress