To amplify Messa, catch what you expect are failure modes that you know how to recover from. Ian Bicking wrote an article that addresses some of the overarching principles as does Eli Bendersky's note.

The problem with the sample code is that it is not handling errors, just prettifying them and discarding them. Your code does not "know" what to do with a NameError and there isn't much it should do other than pass it up, look at Bicking's re-raise if you feel you must add detail.

IOError and OSError are reasonably "expectable" for a shutil.move but not necessarily handleable. And the caller of your function wanted it to move a file and may itself break if that "contract" that Eli writes of is broken.

Catch what you can fix, adorn and re-raise what you expect but can't fix, and let the caller deal with what you didn't expect, even if the code that "deals" is seven levels up the stack in main.

Answer from msw on Stack Overflow
🌐
Tutorial Teacher
tutorialsteacher.com › python › error-types-in-python
Error Types in Python
Learn about built-in error types in Python such as IndexError, NameError, KeyError, ImportError, etc.
🌐
Honeybadger
honeybadger.io › blog › errors-in-python
Errors in Python: Types, Causes, and Examples - Honeybadger Developer Blog
April 27, 2026 - Examples of runtime errors in Python include ZeroDivisionError, NameError, TypeError, and ValueError. Let’s discuss the different runtime errors, their causes, and ways to avoid them.
🌐
Rollbar
rollbar.com › home › what are the different types of python errors? – and how to handle them
Python Errors and How to Handle Them (With Examples)
Now the code will run without any errors, and the output will be 500, which is the element at index 4 of the list. An AttributeError occurs when you try to access an attribute or method that doesn't exist for a particular object type. This often happens due to typos or misunderstanding what methods are available for different data types. Here's an example of an AttributeError in Python...
Published: July 14, 2025
🌐
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu › notebooks › chapter10.01-Error-Types.html
Error Types — Python Numerical Methods
AS shown in the examples above, there are different types of built-in exceptions: ZeroDivisionError, TypeError, and NameError. You can find a complete list of built-in exceptions in the Python documentation. Of course, you can define your own exception types, but we will not deal with this ...
Top answer
1 of 4
12

To amplify Messa, catch what you expect are failure modes that you know how to recover from. Ian Bicking wrote an article that addresses some of the overarching principles as does Eli Bendersky's note.

The problem with the sample code is that it is not handling errors, just prettifying them and discarding them. Your code does not "know" what to do with a NameError and there isn't much it should do other than pass it up, look at Bicking's re-raise if you feel you must add detail.

IOError and OSError are reasonably "expectable" for a shutil.move but not necessarily handleable. And the caller of your function wanted it to move a file and may itself break if that "contract" that Eli writes of is broken.

Catch what you can fix, adorn and re-raise what you expect but can't fix, and let the caller deal with what you didn't expect, even if the code that "deals" is seven levels up the stack in main.

2 of 4
5

Python doesn't have a mechanism right now for declaring which exceptions are thrown, unlike (for example) Java. (In Java you have to define exactly which exceptions are thrown by what, and if one of your utility methods needs to throw another exception then you need to add it to all of the methods which call it which gets boring quickly!)

So if you want to discover exactly which exceptions are thrown by any given bit of python then you need to examine the documentation and the source.

However python has a really good exception hierarchy.

If you study the exception hierarchy below you'll see that the error superclass you want to catch is called StandardError - this should catch all the errors that might be generated in normal operations. Turning the error into into a string will give a reasonable idea to the user as to what went wrong, so I'd suggest your code above should look like

from shutil import move
try:
    move('somefile.txt', '/tmp/somefile.txt')
except StandardError, e:
    print 'Move failed: %s' % e

Exception hierarchy

BaseException
|---Exception
|---|---StandardError
|---|---|---ArithmeticError
|---|---|---|---FloatingPointError
|---|---|---|---OverflowError
|---|---|---|---ZeroDivisionError
|---|---|---AssertionError
|---|---|---AttributeError
|---|---|---BufferError
|---|---|---EOFError
|---|---|---EnvironmentError
|---|---|---|---IOError
|---|---|---|---OSError
|---|---|---ImportError
|---|---|---LookupError
|---|---|---|---IndexError
|---|---|---|---KeyError
|---|---|---MemoryError
|---|---|---NameError
|---|---|---|---UnboundLocalError
|---|---|---ReferenceError
|---|---|---RuntimeError
|---|---|---|---NotImplementedError
|---|---|---SyntaxError
|---|---|---|---IndentationError
|---|---|---|---|---TabError
|---|---|---SystemError
|---|---|---TypeError
|---|---|---ValueError
|---|---|---|---UnicodeError
|---|---|---|---|---UnicodeDecodeError
|---|---|---|---|---UnicodeEncodeError
|---|---|---|---|---UnicodeTranslateError
|---|---StopIteration
|---|---Warning
|---|---|---BytesWarning
|---|---|---DeprecationWarning
|---|---|---FutureWarning
|---|---|---ImportWarning
|---|---|---PendingDeprecationWarning
|---|---|---RuntimeWarning
|---|---|---SyntaxWarning
|---|---|---UnicodeWarning
|---|---|---UserWarning
|---GeneratorExit
|---KeyboardInterrupt
|---SystemExit

This also means that when defining your own exceptions you should base them off StandardError not Exception.

Base class for all standard Python exceptions that do not represent
interpreter exiting.
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.7 documentation
If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with an error message. A try statement may have more than one except clause, to specify handlers for different exceptions. At most one handler will be executed. Handlers only handle exceptions that occur in the corresponding try clause, not in other handlers of the same try statement. An except clause may name multiple exceptions, for example: ... except RuntimeError, TypeError, NameError: ...
🌐
Programiz
programiz.com › python-programming › exceptions
Python Exceptions (With Examples)
To learn more about them, visit Python try, except and finally statements. Errors represent conditions such as compilation error, syntax error, error in the logical part of the code, library incompatibility, infinite recursion, etc. Errors are usually beyond the control of the programmer and ...
🌐
Geek University
geek-university.com › home › types of errors
Types of errors | Python#
February 1, 2022 - C:\Python34\Scripts>python error.py File "error.py", line 3 if x%2 == 0 ^ SyntaxError: invalid syntax · 2. Runtime errors – errors that occur after the code has been compiled and the program is running. The error of this type will cause your program to behave unexpectedly or even crash. An example of an runtime error is the division by zero.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › errors-and-exceptions-in-python
Errors and Exceptions in Python - GeeksforGeeks
April 26, 2025 - In Python, an EOFError is raised when one of the built-in functions, such as input() or raw_input() reaches the end-of-file (EOF) condition without reading any data. This commonly occurs in online IDEs or when reading from a file where there is no more data left to read. Example:Pythonn = int(input( ... In Python, exceptions are errors that occur at runtime and can crash your program if not handled.
🌐
Qodo
qodo.ai › blog › learn › common python error types and how to resolve them
Common Python error types and how to resolve them
March 20, 2025 - IndexError is one of Python’s runtime errors that manifests when code attempts to access a sequence index that exceeds its bounds. You’ll come across this error type mostly when dealing with list operations, string manipulations, and array processing when the requested index falls outside the valid range viz.
🌐
Letshired
letshired.com › tutorials › python › python-error-types
Python Error Types
Errors like SyntaxError, TypeError, and ZeroDivisionError are common, and learning how to handle them with try-except blocks can help prevent crashes and improve user experience. By raising exceptions and using finally for cleanup, you can ensure ...
🌐
Medium
medium.com › @techwithpraisejames › types-of-errors-in-python-and-how-to-handle-them-fe8616257b52
Common Types of Errors in Python and How to Handle Them | by Praise James | Medium
August 7, 2025 - So, the attempt to use ‘numbers’ as a function instead of an indexable list would result in a type error. In Python, a ValueError is raised when a built-in operation or function receives an argument that has the right type but an inappropriate value. You can get a ValueError in various scenarios, such as when you are try to convert a string to an integer, access an index that doesn’t exist in a list, or perform operations with inappropriate values for a specific function or operation. ... In this example, the ‘int()’ function is called with a string that cannot be converted to an integer, resulting in a ValueError.
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › Exceptions › standard-exceptions.html
19.4. Standard Exceptions — Foundations of Python Programming
BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StopIteration +-- StopAsyncIteration +-- ArithmeticError | +-- FloatingPointError | +-- OverflowError | +-- ZeroDivisionError +-- AssertionError +-- AttributeError +-- BufferError +-- EOFError +-- ImportError +-- LookupError | +-- IndexError | +-- KeyError +-- MemoryError +-- NameError | +-- UnboundLocalError +-- OSError | +-- BlockingIOError | +-- ChildProcessError | +-- ConnectionError | | +-- BrokenPipeError | | +-- ConnectionAbortedError | | +-- ConnectionRefusedError | | +-- ConnectionResetError | +-- F
🌐
W3Schools
w3schools.com › python › python_ref_exceptions.asp
Python Built-in Exceptions
The table below shows built-in exceptions that are usually raised in Python.
🌐
Middleware
middleware.io › blog › python-error-types
Python Error Types: Common Errors and How to Handle Them
Dividing any number by zero is mathematically undefined and results in a runtime error. ... OverflowError occurs when a numerical operation results in a number too large to be represented within Python’s numeric limits (mostly for floating-point numbers). Example with the math module’s exponential function:
🌐
Medium
medium.com › @rayancrazer › types-of-errors-in-python-4e8619d133e9
Common Types of Python Errors Explained | Medium
November 20, 2024 - Example of IndexError in Python Let’s consider an example to illustrate how an IndexError can occur in Python. Suppose we have a list of numbers and we want to access the element at index 5. Here’s what the code might look like: In this code, we’re trying to access the element at index 5, which should be the sixth element in the list. However, the list only contains five elements, so there is no element at index 5. When we run this code, we get the following output: This error message tells us that we’ve tried to access an index that is outside the range of valid indexes for the list.
🌐
Tutorialspoint
tutorialspoint.com › python › standard_exceptions.htm
Python Standard Exceptions
Here is a list all the standard Exceptions available in Python −
🌐
DEV Community
dev.to › namimai › python-error-types-explained-troubleshooting-for-beginners-4o76
Python Error Types Explained: Troubleshooting for Beginners - DEV Community
February 23, 2025 - Some common error types in Python are, syntax errors, logic errors, assertion errors, index error, key error, name error, type error. Error types are like clues that guide you on how to fix the problem.