Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exceptions might a Python function raise? [duplicate]

Is there any way in Python to determine what exceptions a (built-in) function might raise? For example, the documentation (http://docs.python.org/lib/built-in-funcs.html) for the built-in int(s) says nothing about the fact that it might raise a ValueError if s is not a validly formatted int.


This is a duplicate of Does re.compile() or any given Python library call throw an exception?

like image 708
tjdonaldson Avatar asked Apr 29 '26 13:04

tjdonaldson


1 Answers

The only way to tell what exceptions something can raise is by looking at the documentation. The fact that the int() documentation doesn't say it may raise ValueError is a bug in the documentation, but easily explained by ValueError being exactly for that purpose, and that being something "everybody knows".

To belabour the point, though, documentation is the only way to tell what exceptions you should care about; in fact, any function can potentially raise any exception, even if it's just because signals may arrive and signal handlers may raise exceptions. You should not anticipate or handle those errors, however; you should just handle the errors you expect.

like image 117
Thomas Wouters Avatar answered May 02 '26 03:05

Thomas Wouters