Resume after exception

Richard Lewis richardlewis at fastmail.co.uk
Tue Jun 14 11:09:30 CEST 2005


Hi there,

Is it possible to have an 'except' case which passes control back to the
point after the exception occurred?

e.g.

# a function to open the file
# raises FileLockedException is file contains 'locked' information
def open_file(file_name):
    f = file(file_name, 'r')
    {read first line for file lock info}
    if first_line == "FILE LOCKED":
        raise FileLockedException(lock_user, lock_timestamp)
    {read remainder of file}
    return True

# elsewhere in a user interface module
def open_command():
    try:
        open_file("foo.bar")
    except FileLockException, X:
        ans = tkMessageBox.askyesno(title="File Locked", message="File
        locked by '" + X.user + "' on " + X.time_stamp + "\nContinue
        anyway?")
        if ans == tkMessageBox.YES:
            # return control to the remainder of the open_file function.
            How?
        else:
            return False

Any ideas?

Cheers,
Richard



More information about the Python-list mailing list