Python: Catching specific exception
in except ValueError,e
, e
is an instance of the exception, not a string. So when you test if e
is not equal to a particular string, that test is always False. Try:
if str(e) != "..."
instead.
Example:
def catch(msg):
try:
raise ValueError(msg)
except ValueError as e: # as e syntax added in ~python2.5
if str(e) != "foo":
raise
else:
print("caught!")
catch("foo")
catch("bar")
Typically, you don't really want to rely on the error message if you can help it -- It's a little too fragile. If you have control over the callable macdat
, instead of raising a ValueError
in macdat
, you could raise a custom exception which inherits from ValueError
:
class MyValueError(ValueError): pass
Then you can only catch MyValueError
and let other ValueError
s continue on their way to be caught by something else (or not). Simple except ValueError
will still catch this type of exception as well so it should behave the same in other code which might also be catching ValueErrors from this function.
Catching specific error messages in try / except
You were close. The trick is to use ValueError as e
and compare your strings against str(e)
. It's also good practice to use if
/ elif
rather than repeated if
statements.
Here's a working example:
import calendar, datetime
try:
print(datetime.date(int(birthYear), int(birthMonth), int(birthDay)))
except ValueError as e:
if str(e) == 'month must be in 1..12':
print('Month ' + str(birthMonth) + ' is out of range. The month must be a number in 1...12')
elif str(e) == 'year {0} is out of range'.format(birthYear):
print('Year ' + str(birthMonth) + ' is out of range. The year must be a number in ' + str(datetime.MINYEAR) + '...' + str(datetime.MAXYEAR))
elif str(e) == 'day is out of range for month':
print('Day ' + str(birthDay) + ' is out of range. The day must be a number in 1...' + str(calendar.monthrange(birthYear, birthMonth)))
How to import and catch module-specific exceptions in Python?
This is how I usually do it:
>>> import multiprocessing
... q = multiprocessing.Queue()
... q.get_nowait()
...
...
---------------------------------------------------------------------------
Empty Traceback (most recent call last)
<...snip...>
>>> import sys
>>> err = sys.last_value
>>> err.__module__
'queue'
>>> from queue import Empty
>>> isinstance(err, Empty)
True
There is no foolproof way that works for all modules in the generic case, because they don't usually know (or care) about all their dependencies exception hierarchy. An exception in 3rd party code would just bubble up the stack, and there is generally no point to catch it unless one can actually do something to handle it and continue. Good projects will usually document the exception hierarchy clearly in their API guide.
Pythonic exception handling: only catching specific errno
I just stumbled across the probably most elegant solution: creating the ignored
context manager:
import errno
from contextlib import contextmanager
@contextmanager
def ignorednr(exception, *errornrs):
try:
yield
except exception as e:
if e.errno not in errornrs:
raise
pass
with ignorednr(OSError, errno.EEXIST):
os.mkdir(dir)
This way I just have the ugly job of creating the context manager once, from then on the syntax is quite nice and readable.
The solution is taken from https://www.youtube.com/watch?v=OSGv2VnC0go.
Handling both specific and general Python exceptions?
You could reraise the exception, and handle the generic case in the outer handler of a nested setup:
try:
try:
output_var = some_magical_function()
except IntegrityError as zde:
integrity_error_handling()
raise
except ALLExceptions as ae: # all exceptions INCLUDING the IntregityError
shared_exception_handling_function(ae) # could be error reporting
The unqualified raise
statement re-raises the current exception, so the IntegrityError
exception is thrown again to be handled by the AllExceptions
handler.
The other path you could take is to test for the exception type:
try:
output_var = some_magical_function()
except ALLExceptions as ae: # all exceptions INCLUDING the IntregityError
if isinstance(ae, IntegrityError):
integrity_error_handling()
shared_exception_handling_function(ae) # could be error reporting
How do I determine what type of exception occurred?
The other answers all point out that you should not catch generic exceptions, but no one seems to want to tell you why, which is essential to understanding when you can break the "rule". Here is an explanation. Basically, it's so that you don't hide:
- the fact that an error occurred
- the specifics of the error that occurred (error hiding antipattern)
So as long as you take care to do none of those things, it's OK to catch the generic exception. For instance, you could provide information about the exception to the user another way, like:
- Present exceptions as dialogs in a GUI
- Transfer exceptions from a worker thread or process to the controlling thread or process in a multithreading or multiprocessing application
So how to catch the generic exception? There are several ways. If you just want the exception object, do it like this:
try:
someFunction()
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print message
Make sure message
is brought to the attention of the user in a hard-to-miss way! Printing it, as shown above, may not be enough if the message is buried in lots of other messages. Failing to get the users attention is tantamount to swallowing all exceptions, and if there's one impression you should have come away with after reading the answers on this page, it's that this is not a good thing. Ending the except block with a raise
statement will remedy the problem by transparently reraising the exception that was caught.
The difference between the above and using just except:
without any argument is twofold:
- A bare
except:
doesn't give you the exception object to inspect - The exceptions
SystemExit
,KeyboardInterrupt
andGeneratorExit
aren't caught by the above code, which is generally what you want. See the exception hierarchy.
If you also want the same stacktrace you get if you do not catch the exception, you can get that like this (still inside the except clause):
import traceback
print traceback.format_exc()
If you use the logging
module, you can print the exception to the log (along with a message) like this:
import logging
log = logging.getLogger()
log.exception("Message for you, sir!")
If you want to dig deeper and examine the stack, look at variables etc., use the post_mortem
function of the pdb
module inside the except block:
import pdb
pdb.post_mortem()
I've found this last method to be invaluable when hunting down bugs.
How can I write a `try`/`except` block that catches all exceptions?
You can but you probably shouldn't:
try:
do_something()
except:
print("Caught it!")
However, this will also catch exceptions like KeyboardInterrupt
and you usually don't want that, do you? Unless you re-raise the exception right away - see the following example from the docs:
try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except IOError as (errno, strerror):
print("I/O error({0}): {1}".format(errno, strerror))
except ValueError:
print("Could not convert data to an integer.")
except:
print("Unexpected error:", sys.exc_info()[0])
raise
Related Topics
Python: Why Does My List Change When I'm Not Actually Changing It
What Does "\R" Do in the Following Script
Export a Pandas Dataframe as a Table Image
How Can a Recursive Regexp Be Implemented in Python
All Synonyms for Word in Python
Pandas To_Datetime Parsing Wrong Year
Correct Style for Python Functions That Mutate the Argument
Qwidget Does Not Draw Background Color
Matplotlib Figure Facecolor (Background Color)
Sparse Matrix Slicing Using List of Int
Why Can't I Change Only a Single Element in a Nested List in Python
How to Set Headers Using Python's Urllib
Python Pyqt Signals Are Not Always Working