0

Take a look at a chunk of code:

move = [None, 20, 5, True, "a"]   
valnum = 0
    for val in move:
        try:
            if val >= 15:
                val -= 3
                move[valnum] = val
        finally:
            valnum += 1
print(move)

When run, it gives this error:

TypeError: '>=' not supported between instances of 'NoneType' and 'int'

Isn't the whole point of the finally statement to avoid errors like this?

EDIT: Thank you for your help. I don't think I made myself clear enough in my question. After your suggestions, I coded it to this:

move = [None, 20, 5, True, "a", 19]   
valnum = 0
for val in move:
    try:
        if val >= 15:
            val -= 3
            move[valnum] = val
    except TypeError:
        valnum += 1
print(move)

And the output now becomes:

[None, 17, 16, True, 'a', 19]

It's easy to see what went wrong. The exception block only runs when there is an error, but I need the block to run no matter if there is an error or not. Is there a solution to this, other than just:

move = [None, 20, 5, True, "a", 19]   
valnum = 0
for val in move:
    try:
        if val >= 15:
            val -= 3
            move[valnum] = val
    except TypeError:
        valnum += 1
        continue
    valnum += 1
print(move)
0

2 Answers 2

2

No, it is not.

It is there to make sure something always gets executed, no matter if there is an exception or not. It does not catch the exception, for that you need an except block.

For example:

for val in move:
    try:
        if val >= 15:
            val -= 3
            move[valnum] = val
    except TypeError:
        valnum += 1

finally blocks are useful for things like resource cleanup, closing files, that sort of thing.


Based on the comment by Big Bird, here is how you would run the code and also ignore the error:

for val in move:
    try:
        if val >= 15:
            val -= 3
            move[valnum] = val
    except TypeError:
        pass  # catch the error and ignore it
    valnum += 1

Edited yet again, I realised what you wanted to do only now, since I'd been too focussed on how you were trying to do it. I suggest not caring about valnum at all, instead doing this:

move = [None, 20, 5, True, "a", 19]   
for index, val in enumerate(move):
    try:
        if val >= 15:
            val -= 3
            move[index] = val
    except TypeError:
        pass
print(move)
2
  • What if I want the valnum += 1 line to run no matter if there is an error or not in the try?
    – Big Bird
    Feb 13, 2022 at 21:09
  • Please see my edited answer to take your comment into account.
    – Jasmijn
    Feb 13, 2022 at 21:47
0

What you want to use is 'except' for handling that. Your code should look like:

move = [None, 20, 5, True, "a"]   
valnum = 0
for val in move:
    try:
        if val >= 15:
            val -= 3
            move[valnum] = val
    except TypeError:
        valnum += 1
print(move)

When you use finally, it means that the code inside finally will run after the try condition as a clean-up action. You can check the documentation for more information on the finally action: https://docs.python.org/3/tutorial/errors.html#defining-clean-up-actions

Looking at your code and use, you can simply

3
  • 1
    Bare excepts are considered harmful
    – Jasmijn
    Feb 13, 2022 at 20:56
  • Thanks for pointing that out, had missed it
    – veedata
    Feb 13, 2022 at 21:00
  • Thanks for the cleanup. But, what if I want the valnum += 1 line to run no matter if there is an error or not in the try?
    – Big Bird
    Feb 13, 2022 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.