Python is a block scoped language, you can't reference a variable outside of the block in which it was defined and you can't use a new value outside of the block in which that variable was updated.
In other words, you can't reference the exception
variable's error data outside of the except
block, if you try to do so, the value of the exception
variable will be None
(which you set at the top-level).
Try moving the contents of your else
block to the except
block and get rid of the exception = None
and if exception
, like this:
timer = Timer(10.0)
while timer.alive:
try:
shutil.rmtree(cls.workspace)
break
except OSError as exception:
raise exception
time.sleep(0.1)
If you don't want fatal errors, you could just use the print()
function instead of the raise
keyword:
timer = Timer(10.0)
while timer.alive:
try:
shutil.rmtree(cls.workspace)
break
except OSError as exception:
print(exception)
time.sleep(0.1)
Here is another example (which won't work):
def hello():
message = "Hello World"
print(message)
Because it'll raise the the following error:
NameError: name 'message' is not defined
NOTE: I'd advice against calling your exception exception
, because there's an error class called Exception
and doing so could lead to confusion later on.
Good luck.