0

Is there any way to programmatically determine which exceptions an object or method might raise?

Like dir(obj) lists available methods, I'm looking for the equivalent dir_exceptions(obj).

As far as I know, the only way to achieve this would be to parse the source.

2
  • Consider that the method might call library functions which themselves might raise arbitrary exceptions. Commented Nov 22, 2011 at 19:13
  • possible duplicate of Python and Exceptions (see comments on Noufal's answer) Commented Nov 22, 2011 at 19:27

4 Answers 4

2

I don't think this is possible. An exception is a runtime phenomenon and you'll know what it possible (or what happens) only while running. Why would you want to do this though?

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

10 Comments

I bet wants to do something like: try { blah() } catch dir_exceptions(blah) { }
It's definitively not possible. In Python it would even be possible to generate the exception class on the fly, based on some strange random data. That makes no sense of course, but it would be possible. ;-)
@Civilian: Python is not Java. The Python equivalent of that is try: blah <newline/tab> except: blah
@nmichaels or (usually) better: except Exception:.
@nmichaels: I'm giving the benefit of doubt and assume he's using braces for blocks simply because they are far easier to write and read in SO comments due to the lack of newlines. At least I do it whenever I need to embed multi-line code snippets in comments. And I don't even know Java ;)
|
2

It looks like you'll have to trust the code's developers on this one: if they did a good job, the method/class documentation should list all the exceptions that could be raised.

Comments

1

No, there is not a practical way to do this.

Most python developers derive from Exception, so if you're not sure, just catch Exception.

try:
    some_secret_code()
except Exception:
    print 'oops, something happened'

If you're thinking that you can import a module and poke around looking for things derived from Exception, that won't quite work either. What about that python nut that does this ->

exec "raise SystemExit()"

I'm not sure that there is a non-practical way to accomplish this.

Comments

-1

I don't think this is possible either, but if you trust that the programmer has named their exceptions with "Exception" or "Error" in the name, then you could do a dir on the class and search for elements that end with "Exception" or "Error". Aside from that (which is pretty hacky in itself), I don't see a straightforward/native/idiomatic way to do this

1 Comment

Exceptions are instances of their own classes, and it would be extremely unusual to make an exception class or instance a member of another class. Usually you create an exception locally and anonymously and raise it immediately: raise ValueError( "illegal value" )

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.