1

I am having trouble understanding how to write a try except block that only runs one block or the other not just half the block and then move to the exception as seen in my example for emphasis I do not wish to run any portion of try if any line in try fails

x = 1
y = 1


try:  
    x = x+1
    print(x)
    x.append(1)
except:
    print(x)

which returns

2
2

instead of returning

1

as I would of expected. This is problematic for me because I was foolishly under the impression that only except block would be executed upon try failure. I am scraping websites using beautiful soup and my allocation of a soup usually will throw the exception and the other block will run but unforeseen errors after this allows some lists to be appended then runs the exception block and appends them again. leaving me with lists of different lengths depending on where they fall within each block.

any help is much appreciated

9
  • 2
    But your try block does fail; x.append(1) raises an exception because x is an integer. Commented Jan 12, 2017 at 16:34
  • @DanielRoseman I dont want any of Try block to run in the event any given portion of it fails, like in my example I want to return 1 not 2 , 2 Commented Jan 12, 2017 at 16:36
  • But that doesn't make sense. It has to run, to know if it fails. Commented Jan 12, 2017 at 16:36
  • 1
    Python can't un-print text that's already been printed and it doesn't remember what values it just incremented so it can un-increment them. If you want to reset program state, you've got to do so yourself manually. Commented Jan 12, 2017 at 16:38
  • Possible duplicate of Django - Rollback save with transaction atomic Commented Jan 12, 2017 at 16:39

3 Answers 3

1

You could reset the computed_result to value of x in except-block on error:

x = 1
y = 1
computed_value = 0

try:  
    computed_value = x + 1
    #print(fallback_var)
    computed_value.append(1)
    print("Exceution succeed: keeping value")
except:
    print("Exceution failed: resetting value")
    computed_value = x
    #print(x)


print(computed_value)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Maurice, I was hoping for something more elegant however I think I can make this work.
More elegant could be a customized Command pattern.
1

Let's go step by step:

Your code correctly executes x=x+1 (now x is 2).

Then it correctly executes print(x) (so it prints 2).

Then it tries to execute x.append(1) (and fails)

Since it has failed to append(1) it enters the except and executes print(x) (so it prints 2)

It outputs exactly what's expected.

Comments

0

This run both.

Your code

try:  
    x = x+1
    print(x)
    x.append(1)
except:
    print(x)

When python start execution, it execute line by line

x = x + 1

After this x become 2.

then

print (x)

Print 2 as first in your output.

x.append(1)

Above statment raise exception which is caught by except clause in your code.

Please remember, x value is already change to 2 in x = x + 1 statment. In except when you do

print (x)

It print 2 again.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.