Unicode error

Thomas Jollans thomas at jollans.com
Fri Jul 23 18:04:40 EDT 2010


On 07/23/2010 11:46 PM, Nobody wrote:
> On Fri, 23 Jul 2010 10:42:26 +0000, Steven D'Aprano wrote:
> 
>> Don't write bare excepts, always catch the error you want and nothing 
>> else.
> 
> That advice would make more sense if it was possible to know which
> exceptions could be raised. In practice, that isn't possible, as the
> documentation seldom provides this information. Even for the built-in
> classes, the documentation is weak in this regard; for less important
> modules and third-party libraries, it's entirely absent.
> 

In practice, at least in Python, it tends to be better to work the
"other way around": first, write code without exception handlers. Test.
If you get an exception, there are really two possible reactions:


 1. "WHAT??"
      => This shouldn't be happening. Rather than catching everything,
         fix your code, or think it through until you reach conclusion
         #2 below.

2. "Ah, yes. Of course. I should check for that."
      => No problem! You're staring at a traceback right now, so you
         know the exception raised.

If you know there should be an exception, but you don't know which one,
it should be trivial to create condition in which the exception arises,
should it not? Then, you can handle it properly, without resorting to
guesswork or over-generalisations.



More information about the Python-list mailing list