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

The division script we have been using includes statements that will fit into each part of a full exception-handling compound statement.

Example script: exceptions_division_calc_try_this

  • Change project’s name from  exceptions_division_calc to exceptions_division_calc_try_this.
  • Update it to match the script below
  • Save the modified script.
  • Click the Send to micro:bit button.
# exceptions_division_calc_try_this

from microbit import *

sleep(1000)

print("Division Calculation")

try:
    n = 20
    d = 5

    q = n / d

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

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

finally:
    print("Try again!")
    print()
  • Check the results in the serial monitor.
  • Verify that, as written,  the script’s output works as expected without reporting errors:

Do you remember how to break the script?

  • Modify the script by setting d equal to 0 with d = 0.
  • Re-flash the script, and verify that it displays the division by zero exception text and its type:  

 

Note that the script did not halt at the exception.  Instead, it displayed the exception, but continued to print “Try again!”

Now let’s break it with text one more time.

  • Set d equal to Hello  this time with d = “Hello”.
  • Re-flash the script and verify that it displays the TypeError exception information: 

Note again that the script did not halt at the statement that caused the exception.  It made it all the way through printing “Try again!”

 


Printer-friendly version
How it Works, and Breaks
Prev
Your Turn: Coder-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