Question about the Exception class

  • Thread starter Carl J. Van Arsdall
  • Start date
C

Carl J. Van Arsdall

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

--

Carl J. Van Arsdall
(e-mail address removed)
Build and Release
MontaVista Software
 
S

Scott David Daniels

Carl said:
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
(e-mail address removed)
 
G

Georg Brandl

Carl said:
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?

Creating your own exception hierarchy and using it consistently can help
you debugging when an exception raised by Python itself occurs. Also, you
can customize your own exception objects (such as, add new instance
attributes).

Georg
 

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

No members online now.

Forum statistics

Threads
473,930
Messages
2,570,072
Members
46,522
Latest member
Mad-Ram

Latest Threads

Top