By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

Assigning different Exception message

P: n/a
try:
self.cursor.execute(sql)
except AttributeError, e:
if e.message == "oracleDB instance has no attribute 'cursor'":
e.message = 'oracleDB.open() must be called before' + \
' oracleDB.query()'
raise AttributeError, e

This code does not re-assign e's message when the conditional is satisfied.
Why not?

regards
Oct 12 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Tor Erik Soenvisen wrote:
try:
self.cursor.execute(sql)
except AttributeError, e:
if e.message == "oracleDB instance has no attribute 'cursor'":
e.message = 'oracleDB.open() must be called before' + \
' oracleDB.query()'
raise AttributeError, e

This code does not re-assign e's message when the conditional is
satisfied. Why not?
It does, but e.args is used to generate the message shown:
>>try:
.... None.not_there
.... except AttributeError, e:
.... e.args = ("whatever",)
.... raise e
....
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
AttributeError: whatever

However, I would prefer to rewrite your snippet along the lines:

try:
cursor = self.cursor
except AttributeError:
raise EriksCustomError("oracleDB.open()...")
else:
cursor.execute(sql)

Peter

Oct 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.