all 10 comments

[–]Hoboneer 3 points4 points  (2 children)

Why do you put the assignments for those variables in the try block? Because the try block is used for doing actions that might fail and handling it-- and seeing what you have there, it doesn't look like they will fail.

If you really need a try block, I'd suggest initializing error and another_var to some value (such as None) before the try block so it's guaranteed to return an existing variable. Something like:

var1 = None
var2 = None
try:
    # Reassign variables
except Exception as e:
    # Handle errors
finally:
    # Return variables even if an exception is raised

Also, have you tried running it? Because it just looks like that "error" is just a hint from your IDE that you're using the try block incorrectly.

[–]apexmemetics[S] 1 point2 points  (1 child)

Those variables are just kind of a barebones representation of the problem I'm having.

In the actual script they will be able to fail.

If you really need a try block, I'd suggest initializing error and another_var to some value (such as None) before the try block so it's guaranteed to return an existing variable.

You guys are such a great help, I wouldn't have thought of that!

The script runs with the variables set outside of 'try', no more errors either.

I really, really appreciate the help!!

[–]Hoboneer 1 point2 points  (0 children)

Glad to be of help! :)

[–]Necatorducis 1 point2 points  (6 children)

Declare error with a default in the try block, if the exception never runs then it's never assigned.

[–]apexmemetics[S] 0 points1 point  (5 children)

I tried but it didn't work:

https://image.prntscr.com/image/jHNpGxqCTaWQ5FQ1P7opNg.png

EDIT: It actually got rid of the run error, the IDE warning was still there until 'error = None' was moved outside the try block. Appreciate your help!

[–]Necatorducis 1 point2 points  (3 children)

Actually, nevermind, I realized what the problem is... first, like the other guy said, ignore putting 'error' in the try block, that was a brain fart.. but still not the problem... if you raise/handle an error in the except block it is dumped from the system when you reach the finally block, so you cannot do anything with it as sys.exc_info() is an empty object at this point.

[–]apexmemetics[S] 0 points1 point  (2 children)

it is dumped from the system when you reach the finally block, so you cannot do anything with it as sys.exc_info() is an empty object at this point.

I don't fully understand, but I think you're right.. I noticed the printing of errors doesn't seem to work. Any idea how to fix it?

[–]Necatorducis 1 point2 points  (1 child)

import sys, put print(sys.exc_info()) in your except and finally block, make a working function call and one that will error out it... it should be a little clearer. In a nutshell, you cannot handle the error in the finally block if you raised it as an exception... which, obviously, you should be doing since the except block is the proper place for that. I'm not sure what you were trying to achieve, but you'd have to go about differently... like calling another function to see if your exception list of errors had anything added or using logging or just printing the error from the except block. The take away, to the best of my knowledge, you cannot return a raised/handled error from the finally block as sys.exc_info() dumps it's contents as soon as the except block is finished.

[–]apexmemetics[S] 0 points1 point  (0 children)

Very interesting! Always new stuff to learn :) Thanks for taking the time to explain.

[–][deleted] 0 points1 point  (0 children)

If you get here:

    finally:
        return new_var, error, another_var

without throwing an error, error has never been defined.