[Tutor] try..except - what about that ton of **Error statements?

Bod Soutar bodsda at googlemail.com
Wed May 22 08:25:52 CEST 2013


On 22 May 2013 07:20, Jim Mooney <cybervigilante at gmail.com> wrote:
>> Keep the try block small. For example if it's for a call to
>> open(filename, "r") the only possible errors (assuming correct syntax)
>> are NameError for using an undefined variable and IOError for
>> specifying a file which doesnt exist.
>
> Thanks. Since I'm new at this the error lists I saw just had the bald
> names, which didn't tell me much. But I found a concise and basic
> explanation of each error at
> http://python.about.com/od/pythonstandardlibrary/a/lib_exceptions.htm
>
> Although it's still a bit circular at my level. I won't be sure what
> errors to raise until I see enough errors, but that will come with
> experience, I guess. Now I just have to figure how to print the list
> out without the ads ;')
>
> Jim
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

One thing I tend to do is fire up the interpreter and deliberately
write something that should fail, then I can see what error is raised.
For example, when I suggested the open() test, I fired up the
interpreter and tried

>>> open(dave, "r")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'dave' is not defined

>>> open("dave2", "r")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'dave2'


More information about the Tutor mailing list