Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: User-Defined Exception That Proves The Rule

Python documentations states:

Exceptions should typically be derived from the Exception class, either directly or indirectly.

the word 'typically' leaves me in an ambiguous state.

consider the code:

class good(Exception): pass
class bad(object): pass

Heaven = good()
Hell = bad()

>>> raise Heaven

Traceback (most recent call last):
  File "<pyshell#163>", line 1, in <module>
    raise Heaven
good

>>> raise Hell

Traceback (most recent call last):
  File "<pyshell#171>", line 1, in <module>
    raise Hell
TypeError: exceptions must be classes or instances, not bad

so when reading the python docs, should i replace 'typically' with ''?

what if i have a class hierarchy that has nothing to do with the Exception class, and I want to 'raise' objects belonging to the hierarchy?

I can always raise an exception with an argument:

raise Exception, Hell

This seems slightly awkward to me

What's so special about the Exception (EDIT: or BaseException) class, that only its family members can be raised?

like image 688
bandana Avatar asked May 08 '10 10:05

bandana


People also ask

What are the user defined exceptions in Python?

User-defined exceptions are created to force certain constraints on the values of the variables. To create a User-defined Exception, we have to create a class that implements the Exception class. We can raise(throw) these exceptions using the raise keyword.

How do you catch an instance of a specific exception type in Python?

Catching Exceptions in Python In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause.

What are user defined exceptions?

User Defined Exception or custom exception is creating your own exception class and throws that exception using 'throw' keyword. This can be done by extending the class Exception. There is no need to override any of the above methods available in the Exception class, in your derived class.


2 Answers

There are other valid classes you can inherit from apart from Exception, for example BaseException.

See the documentation for the exception hierarchy.

BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- StopIteration
      +-- StandardError
      etc..

In older versions of Python it was possible to throw things other than exceptions. For example in Python 2.5:

>>> raise "foo"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
foo

But you get this deprecation warning:

DeprecationWarning: raising a string exception is deprecated

In newer versions this is not allowed. Everything you raise must derive from BaseException.

like image 191
Mark Byers Avatar answered Sep 17 '22 21:09

Mark Byers


"so when reading the python docs, should i change 'typically' with ''?"

No.

Typically, you inherit from Exception. Period. That's what it says.

Sometimes, you might inherit from BaseException. That's what it doesn't say. You might extend BaseExcetion because you want to defeat except Exception handlers.

What's so special about ...

They're subclasses of BaseException. What more do you need to know? The source is readily available. You can read the source code for the raise statement to see exactly what it checks before it throws the TypeError.

http://svn.python.org/view/python/trunk/Python/ceval.c?annotate=80817

Lines 3456 to 3563.

However, all that matters from a practical stand-point is "subclasses of BaseException."

like image 31
S.Lott Avatar answered Sep 19 '22 21:09

S.Lott