[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Replacement for e.message() in python 2.6

Nandakumar Chandrasekhar

2/17/2010 9:59:00 AM

Dear Folks,

In previous versions of Python I used to use e.message() to print out
the error message of an exception like so:

try:
result = x / y
except ZeroDivisionError, e:
print e.message()

Unfortunately in Python 2.6 the message method is deprecated.

Is there any replacement for the message method in Python 2.6 or is
there any best practice that should be used in Python from now on?

Thank you.

Yours sincerely,
Nanda
2 Answers

Arnaud Delobelle

2/17/2010 10:19:00 AM

0

Nandakumar Chandrasekhar <navanitachora@gmail.com> writes:

> Dear Folks,
>
> In previous versions of Python I used to use e.message() to print out
> the error message of an exception like so:
>
> try:
> result = x / y
> except ZeroDivisionError, e:
> print e.message()
>
> Unfortunately in Python 2.6 the message method is deprecated.
>
> Is there any replacement for the message method in Python 2.6 or is
> there any best practice that should be used in Python from now on?

You can just use the __str__() method of the BaseException object for
this. So instead of

print e.message

You can write

print str(e)

which in turn is equivalent to

print e

For more details see PEP 352 (http://www.python.org/dev/peps...)

--
Arnaud

Chris Prinos

2/19/2010 6:24:00 PM

0

On Feb 17, 4:58 am, Nandakumar Chandrasekhar <navanitach...@gmail.com>
wrote:
> Dear Folks,
>
> In previous versions of Python I used to use e.message() to print out
> the error message of an exception like so:
>
> try:
>         result = x / y
> except ZeroDivisionError, e:
>         print e.message()
>
> Unfortunately in Python 2.6 the message method is deprecated.
>
> Is there any replacement for the message method in Python 2.6 or is
> there any best practice that should be used in Python from now on?
>
> Thank you.
>
> Yours sincerely,
> Nanda

try:
        result = x / y
except ZeroDivisionError as e:
        print e

Note different syntax using "except ... as ..."

e.message is deprecated here, but e.args[0] contains the same thing.

see http://docs.python.org/dev/3.0/whatsnew/2.6.htm...
and http://www.python.org/dev/peps...

chris