Why Error is derived from EnvironmentError in shutil.py?

P

Peng Yu

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?
 
A

alex23

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.
 
P

Peng Yu

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?
 
C

Chris Rebert

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top