raise Exception Syntax question

C

chris

hi,
i am wondering if there is a difference between the following
possibilities to raise an exception, the first seems to be in all the
books about python i have but the second gives the same result at least
under Python 2.3.3:
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
Exception: MESSAGE

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
Exception: MESSAGE


is there a preferable way?

thanks
chris
 
P

Peter Hansen

chris said:
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
Exception: MESSAGE


Traceback (most recent call last):
File "<interactive input>", line 1, in ?
Exception: MESSAGE

is there a preferable way?

The latter is preferred by many, but in any case note that the
former is basically translated to the latter by Python itself
when it executes that code so you might as well go with it.
The "except" statement catches an instance, after all, so
explicitly raising one is probably a Good Thing.

-Peter
 
G

Greg Ewing

There's actually no need to import exceptions, since
all the standard exception names are built-in. I
think the exceptions module is just there for
historical compatibility.

This is the preferred form. Again, the other one
only exists for historical reasons.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top