The value exceptions are raised with (odd behaviour?)

O

Ola Natvig

When raising an exception with a value for instance an integer you
assume that when catching the exception the value can be compared with
other integers. That is not what happens.

try:
raise Exception, 12
except Exception, number:
print number == 12
# False

This is because the number variable is a exception instance and not an
integer.

I wonder if I'm the only one which find this behavior odd, and if anyone
knows if there is a way to retrieve the original initialization value of
the exception.


ola
 
O

Ola Natvig

Ola said:
When raising an exception with a value for instance an integer you
assume that when catching the exception the value can be compared with
other integers. That is not what happens.

try:
raise Exception, 12
except Exception, number:
print number == 12
# False

This is because the number variable is a exception instance and not an
integer.

I wonder if I'm the only one which find this behavior odd, and if anyone
knows if there is a way to retrieve the original initialization value of
the exception.

I found out that it's possible to retrive the values the exception was
initialized with through exception.args which is a tuple.
 

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,776
Messages
2,569,603
Members
45,191
Latest member
BuyKetoBeez

Latest Threads

Top