Question about the Exception class

Scott David Daniels scott.daniels at acm.org
Wed Jun 14 17:51:17 EDT 2006


Carl J. Van Arsdall wrote:
> So this is probably a fairly basic question, but help me out because I'm 
> just not lining things up and I'm somewhat new to the world of exception 
> handling.
> 
> What's the benefit to inheriting an exception from and of the available 
> parent exception classes?  Does one subclass have benefits over any 
> other?  Most of what I see involves making a new class and inheriting 
> from Exception so that one can have an exception class with a name of 
> their choosing.  If you didn't care about the name would there be any 
> benefit to making a subclass versus raising StandardError or something 
> else equally vanilla?  Are there any difference to library provided 
> exceptions other than their names?
> 
> -carl
> 
If you create a new exception class that is a subclass of, say,
"ValueError,"  Your exception may have some specific data that
assists you in discovering the source of the problem.  Meanwhile,
any code that says:
     try:
         something()
     except ValueError, error:
         ...
will catch your new exception in the ValueError clause.  Subclasses
of exceptions can be seen as more specific version of the parent.

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list