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

Your Turn: Coder-Friendly Hints

A try…except…else…finally statement can have more than one except.  Also, each except can have one or more statements that address specific errors.  For example, instead of displaying the exception information, you can display a message that explains how to solve the problem.  

  • Change the name of your script from exceptions_division_calc_try_this  to  exceptions_division_calc_your_turn.
  • Replace this block:
except Exception as e:
    print("Exception = ", e)
    et = type(e)
    print("Exception type = ", et)

…with this:

except ZeroDivisionError:
    print("Can't divide by zero.")

except TypeError:
        print("Expected a number.")
  • Click Save to save a copy of your script.
  • Run the script with d = 5.
  • Verify that the script provides a quotient result when when d is set to a non-zero integer, in this case, 5:

Now let’s repeatt the tests three times, changing the value of d each time:

  • Then test dividing by zero with d = 0. 
  • Verify that the script displays “Can’t divide by zero.” when d is set to 0.

 

  • Finally, test it one more time, using d = “Hello”.
  • Verify that the script displays “Expected a number.”

 


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

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2026 Parallax Learn • Built with GeneratePress