Assigning different Exception message

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tor Erik Soenvisen

    #1

    Assigning different Exception message

    try:
    self.cursor.exe cute(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
  • Peter Otten

    #2
    Re: Assigning different Exception message

    Tor Erik Soenvisen wrote:
    try:
    self.cursor.exe cute(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 EriksCustomErro r("oracleDB.ope n()...")
    else:
    cursor.execute( sql)

    Peter

    Comment

    Working...