Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Catching Your Own Errors

An exception doesn't stop only the function that raises it. It goes up to the line that called the function, and if that line isn't in a try block, up to the line that called that one, and so on, along the path that the traceback shows, from the bottom up. The first try on the way catches it. If there's none, the program ends.

def ticket_price(age):
    if age < 0:
        raise ValueError("The age can't be negative.")
    if age < 18:
        return 5
    return 10

def family_price(ages):
    total = 0
    for age in ages:
        total += ticket_price(age)
    return total

try:
    print("Family:", family_price([40, 38, 12]))
    print("Family:", family_price([41, -7]))
except ValueError as error:
    print("Check the ages:", error)
print("Next, please.")
Family: 25
Check the ages: The age can't be negative.
Next, please.

ticket_price(-7) raises the exception. family_price() doesn't catch it, so it stops too: the rest of its loop and its return don't run, and the second print() prints nothing. The exception arrives in the main program, where the except block catches it, and the program goes on with Next, please. Without the try, the program would end with a traceback whose three File lines lead from the main program through family_price to ticket_price.

That's the usual way to share the work. The function that checks the data raises an exception: it knows what's wrong, but not what the program should do about it. The code that calls it decides. A form shows the message and asks again; a program that goes through a list of data skips the bad item and goes on with the next one.

For that, put the try inside the loop, around one item. A try around the whole loop would end the loop at the first bad item.

Instructions

  1. 1

    Here's check_username() from the previous exercise, with a list of usernames that the sign-up form received. The program ends with the exception that check_username() raises for "Al". Catch it inside the loop: put the line with append() into a try block, and in except ValueError as error:, print the name and the message, for example Al: The username is too short. The loop then goes on with the next name, and the program prints:

    Al: The username is too short.
    max power: The username must not contain spaces.
    Members: ['anna_k', 'tom99']

    Keep the list as it is: the exercise also tries other lists.

  2. 2

    Below, print how many usernames were refused: Refused: 2.

Back

9/10

  1. 1. Reading a Traceback
  2. 2. Catching an Error
  3. 3. Which Error?
  4. 4. Quiz: try and except
  5. 5. The Message of an Error
  6. 6. else and finally
  7. 7. Quiz: else and finally
  8. 8. Raising an Error
  9. 9. Catching Your Own Errors
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.