Why Error is derived from EnvironmentError in shutil.py?

Chris Rebert clp2 at rebertia.com
Thu Nov 12 10:37:41 EST 2009


On Thu, Nov 12, 2009 at 7:10 AM, Peng Yu <pengyu.ut at gmail.com> wrote:
> On Thu, Nov 12, 2009 at 12:00 AM, alex23 <wuwei23 at gmail.com> wrote:
>> On Nov 12, 2:46 pm, Peng Yu <pengyu... at gmail.com> wrote:
>>> I see Error is derived from EnvironmentError in shutil.py.
>>>
>>> class Error(EnvironmentError):
>>>     pass
>>>
>>> I'm wondering why EnvironmentError can not be raised directly. Why
>>> Error is raised instead?
>>
>> This way you can explicitly trap on shutil.Error and not intercept any
>> other EnvironmentError that might be raised.
>>
>> And as it's a descendent of EnvironmentError, it can still be caught
>> by any handlers looking for such exceptions.
>
> It is true that an exception class is usually defined in a module and
> all the exceptions raised in this module should be of this derived
> class? Is it the general practice in python?

It is common, but not universal. Several of the modules in the stdlib
use the technique.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list