When working with built-in types like int
and float
in Python, it's common to employ exception handling in cases where input might be unreliable:
def friendly_int_convert(val):
"Convert value to int or return 37 & print an alert if conversion fails"
try:
return int(val)
except ValueError:
print('Sorry, that value doesn\'t work... I chose 37 for you!')
return 37
Are there any prominent edge-cases to be aware of when using str()
?
def friendly_str_convert(val):
"Convert value to str or return 'yo!' & print an alert if conversion fails"
try:
return str(val)
except Exception: # Some specific Exception here
print('Sorry, that value doesn\'t work... I chose \'yo!\' for you!')
return 'yo!'
I really don't like using a broad Exception
since there are cases like NameError
that signify a problem with the code and should raise an error. I've considered UnicodeError
as a candidate but I'm not sure whether str()
causes it (vs. foo.encode()
and foo.decode()
where it's easier to understand) and would love an example of what input, if any, would trigger it.
In summary: Is it generally safe to use str()
without a try
/ except
block even with unreliable input?
In summary: Is it generally safe to use
str()
without atry
/except
block even with unreliable input?
That depends on what kind of input we're talking about. You've tagged this question Python 3, so you don't need to worry about the UnicodeEncodeErrors you'd get with Python 2 and Unicode input, but the object you're receiving could do pretty much anything in its __str__
or __repr__
, raising pretty much any kind of exception. For example,
In [18]: import weakref
In [19]: class Foo(object): pass
In [20]: str(weakref.proxy(Foo()))
---------------------------------------------------------------------------
ReferenceError Traceback (most recent call last)
<ipython-input-20-396b2ab40052> in <module>()
----> 1 str(weakref.proxy(Foo()))
ReferenceError: weakly-referenced object no longer exists
There's a huge difference between str
and int
in this regard. int
can definitely raise TypeError
and ValueError
.
As far as I can think, the only exception that str
can raise for normal objects is UnicodeEncodeError
:
>>> s = u"a\xac\u1234\u20ac\U00008000"
>>> str(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-4: ordinal not in range(128)
And that only happens on python2.x.
Of course, I can easily make a class that fails with just about any exception imaginable:
>>> class MyError(Exception):
... pass
...
>>> class Foo(object):
... def __str__(self):
... raise MyError
...
>>> f = Foo()
>>> str(f)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __str__
__main__.MyError
For the most part, I would question some of the implicit assumptions that all exceptions need to be handled at this point. Generally, it's best to only handle exceptions that you know how to handle. In this case, exotic exceptions that happen because the user put junk into the function should probably be handled at the level where the junk is going in -- not within the function itself. Catching the error and returning some value which is likely nonsense isn't going to be super helpful for debugging issues, etc.
Due to concerns you've raised, I'd do except Exception as e:
. Exception
is generic "catch-all" in Python 3 for "normal" exceptions (other than "system-level" exceptions resulting from process getting signals, KeyboardInterrupt
, etc).
If I were you I'd log the actual exception (e
in my example above) at the very least, to see what actually happen (your code silently drops actual exception object by doing except Exception:
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With