2

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, i.e. 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?

3
  • I usually catch Exception as e while developing and then print(type(e)) to see what I should actually be catching. Commented Jun 1, 2018 at 22:02
  • Does this answer your question? Python When I catch an exception, how do I get the type, file, and line number? Commented Jan 19, 2020 at 13:38
  • @Georgy no, not really. The issue here wasn't general exception catching, but, given that module X throws and error specific to that module, how does one figure out where to import the given exception from X. Commented Jan 20, 2020 at 9:15

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.