Introduction

Failures are part of a program, not an unusual exception to learning. Catch a specific failure near the operation that can fail. Give useful feedback and preserve enough context to recover without silently hiding the problem.

Syntax & example

try:
    number = int("hello")
except ValueError:
    print("Please enter a number.")

Expected output

Please enter a number.

Common mistakes

  • Using a variable before assigning a value to it.
  • Mixing text and numbers without converting deliberately.
  • Skipping an empty input or a boundary value when testing.

Practice

Change the invalid input into a valid number and compare the result.

13px
main.pyYour ideas, in code.
Loading your editor…
Run your code to see the output here.
Ready to buildUTF-84 lines