Home
Search
Error HandlingHandle errors: use the try, except and raise keywords. Use finally to always run code.
Python
This page was last reviewed on Dec 22, 2022.
Error. In Python programs we deal with problems with files, modules and incorrect logic. For some kinds of errors, we must always be prepared.
Some keywords. We use special keywords in Python programs to handle errors. These keywords include the try, except and raise keywords.
An example. Here we introduce a block that follows the try statement. The division expression has zero as the denominator, causing a ZeroDivisionError.
Except In the except statement, we receive the ZeroDivisionError. And we print a special message.
Note This program does not terminate because of this exception. We could continue the program after this point.
try: x = 1 / 0 except ZeroDivisionError: print("Tried to divide by zero")
Tried to divide by zero
Raise. We create exceptions in our code with raise. This program uses the Exception type. The mistake() method creates a custom string based on its parameter. It then raises an exception.
Traceback The Python environment helpfully shows a traceback. This contains the methods called. The mistake() call is shown.
def mistake(name): # Raise an example with custom string. raise Exception(name + " caused exception") # Call method. mistake("Voorheesville")
Traceback (most recent call last): File "C:\file.py", line 4, in <module> mistake("Voorheesville") File "C:\file.py", line 2, in mistake raise Exception(name + " caused exception") Exception: Voorheesville caused exception
Reraise. An exception continues bubbling to the calling methods unless handled. In an except clause, we can use a "raise" statement with no argument. This reraises the exception.
Here The "raise" statement the exception not to be captured. The print-statement is executed before the program terminates.
try: # This causes an exception. f = open("abc") except: print("Except hit") # Raise the exception again. raise
Except hit Traceback (most recent call last): File "C:\programs\file.py", line 6, in <module> f = open("abc") IOError: [Errno 2] No such file or directory: 'abc'
Else. The else-statement can be used after a try-except block. If no exception is thrown, the else-statements are executed. The else must come after the excepts.
Tip In the else, you can perform an action required when no errors are encountered.
Here We show a while-True infinite loop. We accept input from the console, and parse it with the int() built-in method.
Convert
Then We attempt to divide by the number entered. If zero is entered, the except-block is reached. Otherwise, the else is reached.
while True: # Read int from console. denominator = int(input()) # Use int as denominator. try: i = 1 / denominator except: print("Error") else: print("OK")
1 OK 2 OK 0 Error
Finally. This clause is always executed, even if an error is raised. We can use "finally" statements as a way to clean up, or ensure completion of tasks.
Here An error is raised in the try clause. After the "except" clause is executed, the finally clause runs.
try: # An error occurs. x = 1 / 0 except: # Except clause: print("Error encountered") finally: # Finally clause: print("Finally clause reached")
Error encountered Finally clause reached
As-keyword. We can name a variable within an except statement. We use the as-keyword for this. Here we name the IOError "err" and can use it within the clause.
try: f = open("does-not-exist") except IOError as err: # We can use IOError as an instance. print("Error:", err) print("Number:", err.errno)
Error: [Errno 2] No such file or directory: 'does-not-exist' Number: 2
Traceback. When a program crashes, a stack trace (a list of the calling methods) is helpful for debugging. In Python this is called a Traceback. It is a call stack.
Here The "outer" method causes a terrifying ZeroDivisionError. We see the inner() method too in the Traceback.
Lines The Traceback helpfully provides line numbers to aid in our debugging efforts.
def outer(n): return 100 / n def inner(n): return outer(n) # This causes an error. # ... Python provides a stack trace that shows the call stack. inner(0)
Traceback (most recent call last): File "C:\programs\file.py", line 12, in <module> inner(0) File "C:\programs\file.py", line 8, in inner return outer(n) File "C:\programs\file.py", line 5, in outer return 100 / n ZeroDivisionError: division by zero
Benchmark, errors. We can benchmark small programs to determine the speed of constructs. Here we examine the performance of raising exceptions.
Version 1 In this loop, the code causes a ZeroDivisionError on each iteration.
Version 2 In this version of the code, we test against zero with an if-statement, and no exception is raised.
If
Result The loop body that uses exception-handling is much slower. The exception is raised on each iteration through the loop.
While
Tip Avoiding exceptions, as by checking a denominator against zero, often leads to performance advantages.
import time print(time.time()) # Version 1: cause exception. v = 0 i = 0 while i < 10000000: try: x = 10 / v except ZeroDivisionError: x = 0 i += 1 print(time.time()) # Version 2: use if-check. v = 0 i = 0 while i < 10000000: if v != 0: x = 10 / v else: x = 0 i += 1 print(time.time())
1346178493.989 1346178499.7 (Version 1 = 5.711 s) 1346178501.788 (Version 2 = 2.088 s)
Assert. With assert() we cause an AssertionError when a condition is not true. So we "assert" that conditions are valid as the program runs. These statements can be optimized out.
assert
Errors. Many specific errors occur in Python programs. Even aspects of a program such as its indentation leads to errors. Other problems, such as invalid method calls, also provoke trouble.
assert
IndentationError
KeyError
Import
SyntaxError
A note. Exceptions provide a cleaner, simpler way to handle certain types of errors. They streamline programs. They eliminate the burden of excessive error checks in important logic.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2023 Sam Allen.