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

Raising an Error

So far, Python has raised the exceptions. You can raise one yourself, with raise. That's useful in your own function, when it gets data that make no sense. Remember the function for the price of a zoo ticket from the lesson on functions? Here, a negative age raises a ValueError:

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

print(ticket_price(30))
print(ticket_price(-3))
10
Traceback (most recent call last):
  File "<exec>", line 11, in <module>
  File "<exec>", line 3, in ticket_price
ValueError: The age can't be negative.

raise ValueError("The age can't be negative.") creates a ValueError with your message and raises it. From there on, it works like the exceptions that Python raises: the function stops right away (as with return, the rest of it doesn't run), and unless something catches the exception, the program ends with a traceback. Its last line is your type and your message, and the line above it shows where you raised it.

Why not print a message and return something, such as 0? Because the code that called the function couldn't tell. A ticket for 0 looks like the free ticket of a small child, and the program would go on with a wrong price. An exception can't be overlooked: the code that calls the function either catches it and deals with the problem, or the program stops and shows where.

Pick the type that fits the problem. A ValueError is for a value of the right type that makes no sense, such as a negative age or a username that is too short, and it's the one you'll raise most often. Write the message for the person who will read it: say what's wrong.

A typical mistake is to leave out the type: raise "The age can't be negative." ends with TypeError: exceptions must derive from BaseException, because only an exception can be raised, not a text.

Instructions

  1. 1

    The code below the function calls check_username() in a try block and prints the message of a ValueError. On this website, a username needs at least 3 characters. In check_username(), before the return, raise a ValueError with the message The username is too short. when name has fewer than 3 characters. The program then prints Sorry: The username is too short. The exercise also tries other usernames in place of "Al", so keep that line.

  2. 2

    A username must not contain spaces either. Add a second check: when there's a space in name, raise a ValueError with the message The username must not contain spaces. Try it with "max power" in place of "Al", and then change it back.

Back

8/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.