3

I've looked at other questions regarding this particular error but in those cases there was a route in the process where the variable is never defined and hence fails when called later. However with my program I set the variables before the try method, have those variables changed in the try method under certain criteria, and then an if statement calls them later but fails. When Python reaches the if statement and tries to create the variable text, it claims the variables error and error1 are undefined (referenced before assignment).

err = 0
error = 'No Error Set'
error1 = 'No Error1 Set'
try:
    conn = sqlite3.connect(db_dir)
    for row in conn.execute("select max(date) from eod"):
       mxdate = row
    if mxdate == None:
        pass
    else:
        for row in conn.execute('select date, status from eod where date = ?',(mxdate,)):
            _lst_eod = row
    for row in conn.execute("select * from temp"):
        _crnt_eod = row
    conn.close()
except Exception as error:
    error = str(error)
    err = 1
    logged = 0
    try:
        conn.close()
        time = str(right_datetime())[11:19].replace(':','')
        conn = sqlite3.connect(db_dir)
        conn.execute("insert into error_log (date, user, auth, error, _action) values (?,?,?,?,'Failed to select from eod/temp tables.')",(int(str(_date)+time),cred[0],cred[1],error,))
        conn.commit()
        conn.close()
        logged = 1
    except Exception as error1:
        error1 = str(error1)
if err == 1:
    #An error occured here.
    text = '##Error## An error occured while trying to end the day:\n'+error
    if logged == 0:
        text = text+'\n\nA row was written to the error log.'
    else:
        text = text+'\n\nWrite to Error Log failed due to error:\n'+error1
else:
    ....carry on with the rest of the program.
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
I_do_python
  • 1,366
  • 4
  • 16
  • 31

1 Answers1

9

In Python 3, the variable to which you assign the exception in a except SomeException as e: statement is deleted on exiting the except block:

>>> e = "something"
>>> try:
...     1/0
... except ZeroDivisionError as e:
...     print(e)
... 
division by zero
>>> e
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'e' is not defined

The same thing's happening in your code: the except Exception as error: and except Exception as error1: blocks are deleting error and error1 when they exit.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • This seems to be the problem. Changed it to `Exception as _error:` then `error = str(_error)`. I guess when you state `Exception as something`, the _something_ only exists within the try method? – I_do_python Jun 29 '14 at 14:02
  • 1
    Yes ... there's an explanation of what happens in the section of PEP 3110 I link to in my answer. – Zero Piraeus Jun 29 '14 at 14:04
  • @I_do_python `Exception as _something_` something only exists within the except statement. ie when an error occurs that gets caught by the except – Shadow9043 Jun 29 '14 at 14:04