-1
try:
    ...
except (SomeError) as err:
    ...
else:
    ...
finally:
    if err:
   ...

This gives an error: 'err not defined'. Because the exception argument - err - is not defined as far as the finally block is concerned. It appears then that the exception argument is local to the exception block.

You can get round it by copying err to another variable defined outside the block:

teleport = ""
try:
    ...
except (SomeError) as err:
    teleport = err
else:
    ...
finally:
    if teleport:
        ...

But why can't you simply reference the exception argument in the finally block? (Assuming I've not overlooked something else.)

markling
  • 1,232
  • 1
  • 15
  • 28

2 Answers2

3

try blocks will execute code that could possibly raise an exception. except block will execute the moment an exception is raised. The else block executes if no except is raised, and the finally block is executed no matter what.

There is no point in checking for an exception in the finally block when you can just do that in the else block.

Aside from that, the variable was likely garbage collected at the end of execution of the except block. It's similar to what happens with with blocks. This is why you can't do if err:

Zizouz212
  • 4,908
  • 5
  • 42
  • 66
  • `with` blocks don't garbage collect after execution. Try it: `with open("a.txt", "w') as a: pass` `a` – Navith Jun 16 '15 at 01:02
  • Well, yes they don't garbage collect, but I believe they use context managers or something like that. In essence, it would call `a.close()` something like that – Zizouz212 Jun 16 '15 at 01:03
  • 1
    `The else block executes if no except is raised` so no point either in checking there. – Pynchia Jun 16 '15 at 01:29
  • This is not an answer to the question. It's another question, disguised as a smarty-pants answer. Since you ask, I was querying a web page: I wanted to process the status code, regardless of whether it was in error. I decided to use the same variable to catch it in event of error (except) or not (else). That made my processing (finally) simpler, neater. The 'variable not defined' error was raised if I attempted to refer to the exception argument in the finally block. Hence I was interested to know why it worked this way. I know how it works, thank you. My question was why it work's that way. – markling Jun 18 '15 at 09:30
  • If your question was *why it work's that way*, then I've answered your question in my last paragraph. – Zizouz212 Jun 18 '15 at 11:01
  • Ah, you left your answer to your last paragraph! You were making your answer as an afterthought? It is not at all clear what you are trying to say. Would you be kind enough to edit your answer to clarify it? – markling Jun 18 '15 at 14:31
  • If it were an afterthought, I don't think I would've included it. All components are in order for a reason, to ensure the flow and comprehension of every that you should know. I've explained to you everything about exception catching in python, and where to put what and where. – Zizouz212 Jun 18 '15 at 19:26
0

You cannot access just because exception is not raised and so variable is not defined, hence the undefined variable error. Besides there is no point in dealing with exception in your final block, you should do that stuff in except block itself.

hspandher
  • 15,934
  • 2
  • 32
  • 45
  • Although a pretty good guess, and this would be right in many instances, this doesn't look like it. Try raising an exception yourself. – Zizouz212 Jun 18 '15 at 19:27
  • What are you saying, if exception is not raised else block would be execired, and obviously err variable would not be initialised. – hspandher Jun 18 '15 at 19:30
  • Raise an exception, then don't raise an exception. Try it out. – Zizouz212 Jun 18 '15 at 19:31
  • Ok, I would check it out when I would have access to a computer, but why would you want to access exception in case of finally. That clearly smells fishy. – hspandher Jun 18 '15 at 19:33
  • There are many reasons why, but the main uses of `finally` blocks is for cleanup actions, or mandatory operations. – Zizouz212 Jun 18 '15 at 19:42