Execute Program

Python for Programmers: Raising Exceptions

Welcome to the Raising Exceptions lesson!

This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!

  • When something goes wrong, Python indicates the error by raising ("throwing") an exception. Execution stops, and Python prints a message telling us what happened.

  • For example, invalid syntax raises an exception. (You can type error when a code example will cause an error.)

  • >
    sum = 2 +*/- 3
    Result:
    SyntaxError: invalid syntax (<string>, line 1)Pass Icon
  • We can raise our own exceptions with a raise statement. raise SomeExceptionType(some_message) raises the provided exception. Most exceptions take one argument: a message describing what happened.

  • In the next example, we raise ValueError("Incorrect password"). ValueError is one of Python's many built-in exception types.

  • >
    provided_password = "hunter3"
    correct_password = "hunter2"

    if provided_password != correct_password:
    raise ValueError("Incorrect password")
    "Access granted"
    Result:
    ValueError: Incorrect passwordPass Icon
  • Exceptions are values, just like 12 or "abc". Simply creating a ValueError doesn't raise it, and it won't stop execution.

  • >
    provided_password = "hunter3"
    correct_password = "hunter2"

    if provided_password != correct_password:
    # Create an exception, but don't raise it. An easy mistake to make!
    ValueError("Incorrect password")
    "Access granted"
    Result:
    'Access granted'Pass Icon
  • Python has a rich set of built-in exception types. Here are some examples:

  • TypeError means that we provided data of the wrong type. For example, adding a number to a string raises a TypeError exception.

  • >
    "a" + 1
    Result:
    TypeError: can only concatenate str (not "int") to strPass Icon
  • ValueError means that an argument had the correct type, but an incorrect value.

  • >
    my_number = 5
    if my_number % 2 != 0:
    raise ValueError("expected an even number")
    my_number / 2
    Result:
    ValueError: expected an even numberPass Icon
  • ZeroDivisionError means that we tried to divide by zero.

  • >
    5 / 0
    Result:
    ZeroDivisionError: division by zeroPass Icon
  • There are many more built-in exception types. We won't go through all of them here, but several will show up throughout this course.

  • Python also has a generic exception type, Exception.

  • >
    raise Exception("some generic error")
    Result:
    Exception: some generic errorPass Icon
  • However, it's better to be specific when possible. ValueError makes sense in many situations, since most errors are caused by an incorrect value of some kind.

  • Later in the course, we'll define our own exception types. But we need to see many more Python features before we get there.

  • Finally, why does Python talk about "raising" exceptions, when most programming languages talk about "throwing" them? There's no deep reason behind it. It's just a difference in terminology, like Python having "lists" instead of "arrays".