Exceptions Python
Introduction
If there is an error in the code that the interpreter sees, an exception is raised, a so-called Exception Object is created, execution stops, and a Traceback is shown in the terminal.
In English the phrase Raise Exception is used
An exception that the developer did not provide for is called unhandled (Unhandled Exception)
Such behaviour is not always optimal. Not all errors should stop the code.
Perhaps somewhere the developer expects errors to appear and they can be handled differently.
try and except are needed first of all so that the code reacts correctly to possible errors and keeps running where the appearance of an error is not critical.
An exception that is provided for in the code is called handled (Handled)
The try except block has the following syntax
try: pass except Exception: pass else: pass finally: pass
In this article I created the file try_except.py where I copy the code from the examples.
Example
Let us try to open a non-existent file and use the base Exception
try: f = open('missing.txt') except Exception: print('ERR: File not found')
python try_except.py
ERR: No missing.txt file found
The error is caught, our message is shown instead of a Traceback
Let us check that when the file exists everything is fine
try: f = open('existing.txt') except Exception: print('ERR: File not found')
python try_except.py
Emptiness means success
Two exceptions
If there is more than one error, additional exceptions are needed. Let us try to open an existing file and after that add an error.
try: f = open('existing.txt') x = bad_value except Exception: print('ERR: File not found')
python try_except.py
ERR: File not found
The file opened, but since there is an error in the next line, a misleading message appeared in the terminal. The problem is not the "File not found" message but that bad_value is not defined anywhere.
Misleading messages can be avoided by specifying the type of the expected error. In this example it is FileNotFoundError
try: # expected exception f = open('existing.txt') # unexpected exception should result in Traceback x = bad_value except FileNotFoundError: print('ERR: File not found')
python try_except.py
Traceback (most recent call last): File "/home/andrei/python/try_except2.py", line 5, in <module> x = bad_value NameError: name 'bad_value' is not defined
The second error is not caught, therefore a Traceback is shown
Both errors can be caught by adding a second Exception
try: # expected exception should be caught by FileNotFoundError f = open('missing.txt') # unexpected exception should be caught by Exception x = bad_value except FileNotFoundError: print('ERR: File not found') except Exception: print('ERR: Something unexpected went wrong')
python try_except.py
ERR: File not found
ERR: Something unexpected went wrong
Printing the error text
Instead of your own text you can output the text of the error. Let us try with an existing file; there should be one caught error.
try: # expected exception should be caught by FileNotFoundError f = open('existing.txt') # unexpected exception should be caught by Exception x = bad_value except FileNotFoundError as e: print(e) except Exception as e: print(e)
python try_except.py
name 'bad_value' is not defined
Now let us try to open a non-existent file; there should be two caught errors.
try: # expected exception should be caught by FileNotFoundError f = open('missing.txt') # unexpected exception should be caught by Exception x = bad_value except FileNotFoundError as e: print(e) except Exception as e: print(e)
python try_except.py
name 'bad_value' is not defined
[Errno 2] No such file or directory: 'missing.txt'
else
The block else will be executed if no exceptions are caught.
Let us try to open an existing file
existing.txt
which contains the line
www.heihei.ru
try: f = open('existing.txt') except FileNotFoundError as e: print(e) except Exception as e: print(e) else: print(f.read()) f.close()
python try_except.py
www.heihei.ru
If you try to open a non-existent file missing.txt then the exception is handled, and the code from the block else is not executed.
[Errno 2] No such file or directory: 'missing.txt'
finally
The block finally will be executed regardless of whether the exception is caught or not
try: f = open('existing.txt') except FileNotFoundError as e: print(e) except Exception as e: print(e) else: print(f.read()) f.close() finally: print("Finally!")
www.heihei.ru Finally!
And if you try to open a non-existent missing.txt
[Errno 2] No such file or directory: 'missing.txt' Finally!
When to use finally:
Let us consider a script that makes some changes to the system.
Then it tries to do something. At the end it returns the system to its original state.
If an error occurs in the middle of the script, it can no longer return the system to its original state.
But if you move the return to the original state into a finally block, it will work even if there is an error in the previous block.
import os def make_at(path, dir_name): original_path = os.getcwd() os.chdir(path) os.mkdir(dir_name) os.chdir(original_path)
This script will not return to the original directory if there is an error in os.mkdir(dir_name)
And the script below has no such problem
def make_at(path, dir_name): original_path = os.getcwd() os.chdir(path) try: os.mkdir(dir_name) finally: os.chdir(original_path)
It will not be superfluous to add exception handling and output
import os import sys def make_at(path, dir_name): original_path = os.getcwd() os.chdir(path) try: os.mkdir(dir_name) except OSError as e: print(e, file=sys.stderr) raise finally: os.chdir(original_path)
By default print() outputs to sys.stdout, but in the case of exceptions it is more logical to output to sys.stderr
raise
You can raise exceptions manually anywhere in the code using raise.
try: f = open('outdated.txt') if f.name == 'outdated.txt': raise Exception except FileNotFoundError as e: print(e) except Exception as e: print('File is outdated!') else: print(f.read()) f.close() finally: print("Finally!")
python try_except.py
File is outdated! Finally!
raise
can be used to re-raise an exception, for example, to get away from using error codes.
For this it is enough to call raise without arguments; the current exception will be raised.
Example 2
Let us consider a function that takes numbers written out in words and returns them in digits
DIGIT_MAP = { 'zero': '0', 'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9', } def convert(s): number = '' for token in s: number += DIGIT_MAP[token] x = int(number) return x
python
>>> from exc1 import convert >>> convert("one three three seven".split()) 1337
Now let us pass an argument that is not provided for in the dictionary
>>> convert("something unseen".split()) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/andrei/python/exc1.py", line 17, in convert number &plu= DIGIT_MAP[token] KeyError: 'something'
KeyError is the type of the Exception object. The full list can be studied at the end of the article.
The exception took the following path:
REPL → convert() → DIGIT_MAP("something") → KeyError
This exception can be handled by making changes to the convert function
convert(s): try: number = '' for token in s: number += DIGIT_MAP[token] x = int(number) print("Conversion succeeded! x = ", x) except KeyError: print("Conversion failed!") x = -1 return x
>>> from exc1 import convert >>> convert("one nine six one".split()) Conversion succeeded! x = 1961 1961 >>> convert("something unseen".split()) Conversion failed! -1
This handling does not help if you pass an int instead of an iterable object
>>> convert(2022) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/andrei/python/exc1.py", line 17, in convert for token in s: TypeError: 'int' object is not iterable
We need to add TypeError handling
… except KeyError: print("Conversion failed!") x = -1 except TypeError: print("Conversion failed!") x = -1 return x
>>> from exc1 import convert >>> convert("2022".split()) Conversion failed! -1
Let us get rid of the repetition by removing the prints, combining the two exceptions into a tuple, and move the assignment of the value x out of the try block.
Let us also add
a docstring
with a description of the function.
def convert(s): """Convert a string to an integer.""" x = -1 try: number = '' for token in s: number += DIGIT_MAP[token] x = int(number) except (KeyError, TypeError): pass return x
>>> from exc4 import convert >>> convert("one nine six one".split()) 1961 >>> convert("bad nine six one".split()) -1 >>> convert(2022) -1
The errors are handled, but without prints the process is not very informative.
You can show the text of error messages properly by importing sys and changing the function
import sys DIGIT_MAP = { 'zero': '0', 'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9', } def convert(s): """Convert a string to an integer.""" try: number = '' for token in s: number += DIGIT_MAP[token] return(int(number)) except (KeyError, TypeError) as e: print(f"Conversion error: {e!r}", file=sys.stderr) return -1
>>> from exc1 import convert >>> convert(2022) Conversion error: TypeError("'int' object is not iterable") -1 >>> convert("one nine six one".split()) 1961 >>> convert("bad nine six one".split()) Conversion error: KeyError('bad')
The errors are handled and their text is visible in the terminal.
With
!r
you output the
repr()
of the error
raise instead of an error code
In the previous example we relied on returning the number -1 as an error code.
Let us add the function string_log() to the example code and work with it
def string_log(s): v = convert(s) return log(v)
>>> from exc1 import string_log >>> string_log("one two eight".split()) 4.852030263919617 >>> string_log("bad one two".split()) Conversion error: KeyError('bad') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/andrei/exc1.py", line 32, in string_log return log(v) ValueError: math domain error
convert() returned -1, and string_log tried to process it and could not.
You can replace return -1 with raise. This is considered a more correct approach in Python
def convert(s): """Convert a string to an integer.""" try: number = '' for token in s: number += DIGIT_MAP[token] return(int(number)) except (KeyError, TypeError) as e: print(f"Conversion error: {e!r}", file=sys.stderr) raise
>>> from exc7 import string_log >>> string_log("one zero".split()) 2.302585092994046 >>> string_log("bad one two".split()) Conversion error: KeyError('bad') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/andrei/exc7.py", line 31, in string_log v = convert(s) File "/home/andrei/exc7.py", line 23, in convert number += DIGIT_MAP[token] KeyError: 'bad'
Example 3
Let us consider an algorithm for finding the square root
def sqrt(x): """Compute square roots using the method of Heron of Alexandria. Args: x: The number for which the square root is to be computed. Returns: The square root of x. """ guess = x i = 0 while guess * guess != x and i < 20: guess = (guess + x / guess) / 2.0 i += 1 return guess def main(): print(sqrt(9)) print(sqrt(2)) if __name__ == '__main__': main()
python sqrt_ex.py
3.0 1.414213562373095
When trying to compute the root of -1 we get an error
def main(): print(sqrt(9)) print(sqrt(2)) print(sqrt(-1))
python sqrt_ex.py
3.0 1.414213562373095 Traceback (most recent call last): File "/home/andrei/sqrt_ex.py", line 26, in <module> main() File "/home/andrei/sqrt_ex.py", line 23, in main print(sqrt(-1)) File "/home/andrei/sqrt_ex.py", line 16, in sqrt guess = (guess + x / guess) / 2.0 ZeroDivisionError: float division by zero
In the line
guess = (guess + x / guess) / 2.0
Division by zero occurs
It can be handled as follows:
def main(): try: print(sqrt(9)) print(sqrt(2)) print(sqrt(-1)) except ZeroDivisionError: print("Cannot compute square root " "of a negative number.") print("Program execution continues " "normally here.")
Note that all calls of the function are placed in try
python sqrt_ex.py
3.0 1.414213562373095 Cannot compute square root of a negative number. Program execution continues normally here.
If you try to divide by zero several times, one exception is raised and everything after it in the try block will not be executed
def main(): try: print(sqrt(9)) print(sqrt(-1)) print(sqrt(2)) print(sqrt(-1))
python sqrt_ex.py
3.0 Cannot compute square root of a negative number. Program execution continues normally here.
Each attempt to compute the root of -1 will have to be handled separately. This seems inconvenient, but that is the point: every place where you expect an exception must be placed in its own try except block.
The exception can be handled like this:
try: while guess * guess != x and i < 20: guess = (guess + x / guess) / 2.0 i += 1 except ZeroDivisionError: raise ValueError() return guess def main(): print(sqrt(9)) print(sqrt(-1))
python sqrt_ex.py
3.0 Traceback (most recent call last): File "/home/andrei/sqrt_ex3.py", line 17, in sqrt guess = (guess + x / guess) / 2.0 ZeroDivisionError: float division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/andrei/sqrt_ex3.py", line 30, in <module> main() File "/home/andrei/sqrt_ex3.py", line 25, in main print(sqrt(-1)) File "/home/andrei/sqrt_ex3.py", line 20, in sqrt raise ValueError() ValueError
It is much more logical to raise the exception right when the argument is received
def sqrt(x): """Compute square roots using the method of Heron of Alexandria. Args: x: The number for which the square root is to be computed. Returns: The square root of x. Raises: ValueError: If x is negative """ if x < 0: raise ValueError( "Cannot compute square root of " f"negative number {x}") guess = x i = 0 while guess * guess != x and i < 20: guess = (guess + x / guess) / 2.0 i += 1 return guess def main(): print(sqrt(9)) print(sqrt(-1)) print(sqrt(2)) print(sqrt(-1)) if __name__ == '__main__': main()
python sqrt_ex.py
3.0 Traceback (most recent call last): File "/home/avorotyn/python/lessons/pluralsight/core_python_getting_started/chapter8/sqrt_ex4.py", line 35, in <module> main() File "/home/avorotyn/python/lessons/pluralsight/core_python_getting_started/chapter8/sqrt_ex4.py", line 30, in main print(sqrt(-1)) File "/home/avorotyn/python/lessons/pluralsight/core_python_getting_started/chapter8/sqrt_ex4.py", line 17, in sqrt raise ValueError( ValueError: Cannot compute square root of negative number -1
So far it has not turned out very well: the Traceback is visible
You can remove the Traceback by adding ValueError handling to the function calls
import sys def sqrt(x): """Compute square roots using the method of Heron of Alexandria. Args: x: The number for which the square root is to be computed. Returns: The square root of x. Raises: ValueError: If x is negative """ if x < 0: raise ValueError( "Cannot compute square root of " f"negative number {x}") guess = x i = 0 while guess * guess != x and i < 20: guess = (guess + x / guess) / 2.0 i += 1 return guess def main(): try: print(sqrt(9)) print(sqrt(2)) print(sqrt(-1)) print("This is never printed") except ValueError as e: print(e, file=sys.stderr) print("Program execution continues normally here.") if __name__ == '__main__': main()
python sqrt_ex.py
3.0 1.414213562373095 Cannot compute square root of negative number -1 Program execution continues normally here.
Exceptions that should not be handled
IndentationError, SyntaxError and NameError should be fixed in the code rather than handled.
It is important to remember that using exception handling to hush up programmer errors is unacceptable.
List of exceptions
List of exceptions built into Python
The following types of Exception objects exist
BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StopIteration +-- StopAsyncIteration +-- ArithmeticError | +-- FloatingPointError | +-- OverflowError | +-- ZeroDivisionError +-- AssertionError +-- AttributeError +-- BufferError +-- EOFError +-- ImportError | +-- ModuleNotFoundError +-- LookupError | +-- IndexError | +-- KeyError +-- MemoryError +-- NameError | +-- UnboundLocalError +-- OSError | +-- BlockingIOError | +-- ChildProcessError | +-- ConnectionError | | +-- BrokenPipeError | | +-- ConnectionAbortedError | | +-- ConnectionRefusedError | | +-- ConnectionResetError | +-- FileExistsError | +-- FileNotFoundError | +-- InterruptedError | +-- IsADirectoryError | +-- NotADirectoryError | +-- PermissionError | +-- ProcessLookupError | +-- TimeoutError +-- ReferenceError +-- RuntimeError | +-- NotImplementedError | +-- RecursionError +-- SyntaxError | +-- IndentationError | +-- TabError +-- SystemError +-- TypeError +-- ValueError | +-- UnicodeError | +-- UnicodeDecodeError | +-- UnicodeEncodeError | +-- UnicodeTranslateError +-- Warning +-- DeprecationWarning +-- PendingDeprecationWarning +-- RuntimeWarning +-- SyntaxWarning +-- UserWarning +-- FutureWarning +-- ImportWarning +-- UnicodeWarning +-- BytesWarning +-- EncodingWarning +-- ResourceWarning
IndexError
Objects that support the
protocol
Sequence must raise an IndexError when a non-existent index is used.
IndexError, like
KeyError
belongs to the lookup errors LookupError
Example
>>> a = [0, 1, 2] >>> a[3]
Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range
ValueError
ValueError is raised when the object is of the correct type but contains an incorrect value
>>> int("text")
Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'text'
KeyError
KeyError is raised when a search by keys gives no result
>>> sites = dict(urn=1, heihei=2, eth1=3) >>> sites["topbicycle"]
Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'topbicycle'
TypeError
TypeError is raised when an object of a certain type is needed for the successful execution of an operation, but another type is provided.
pi = 3.1415 text = "Pi is approximately " + pi
python str_ex.py
Traceback (most recent call last): File "str_ex.py", line 3, in <module> text = "Pi is approximately " + pi TypeError: can only concatenate str (not "float") to str
Example from the article str()
SyntaxError
SyntaxError is raised when an error is made in the language syntax, for example, a non-existent operator is used.
Python 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> >>> 0 <> 0 File "<stdin>", line 1 0 <> 0 ^ SyntaxError: invalid syntax
Example from the article __future__
Article author: Andrei Olegovich