简体   繁体   中英

How to import and catch module-specific exceptions in Python?

Python modules often have their own exceptions. I often find myself wanting to import those exceptions to be able to properly catch them (properly as in not just cacthing Exception and hoping for the best).

However, I often find myself spending time to figure out exactly where in a module the exceptions are located, or if they're imported from another module, etc. I'm curious if there's a general way to find this out, ie given SomeModulespecificException is there a simple way to figure out how to import it?

Here's an example from the multiprocessing module:

import multiprocessing
q = multiprocessing.Queue()
q.get_nowait()

The above code raises an Empty Exception. In this case, I found out from this answer that the exception is imported from the Queue module, so in this particular case, you need from Queue import Empty to import the exception.

Is there an easy way to figure this out in the general case?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM