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

Tyler Cowan
  • 820
  • 4
  • 13
  • 35
  • 2
    But your try block *does* fail; `x.append(1)` raises an exception because x is an integer. – Daniel Roseman Jan 12 '17 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 – Tyler Cowan Jan 12 '17 at 16:36
  • But that doesn't make sense. It has to run, to know if it fails. – Daniel Roseman Jan 12 '17 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. – Kevin Jan 12 '17 at 16:38
  • Possible duplicate of [Django - Rollback save with transaction atomic](http://stackoverflow.com/questions/34730385/django-rollback-save-with-transaction-atomic) – Tagc Jan 12 '17 at 16:39
  • @DanielRoseman yes I understand that, I was hoping there is a way to step back and move to except and ignore what has happened in Try – Tyler Cowan Jan 12 '17 at 16:40
  • Python has a library called "antigravity". You can suggest incorporating another useful library called "Time Travel To Undo Stuff" XD –  Jan 12 '17 at 16:41
  • @SembeiNorimaki not exactly helpful... I don't think it is that unreasonable that a block of code wrapped within a try statement can be ignored upon failure I am just unsure of how to do it – Tyler Cowan Jan 12 '17 at 16:51
  • It was just a comment to add a little bit of humour. @Maurice Meyer has provided an answer that does what you need. Basically use temporal variables to store results that might need to be reverted. –  Jan 12 '17 at 16:53

3 Answers3

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.

Tagc
  • 8,736
  • 7
  • 61
  • 114
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)
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
  • Thanks Maurice, I was hoping for something more elegant however I think I can make this work. – Tyler Cowan Jan 12 '17 at 17:08
  • 1
    More elegant could be a customized [Command pattern](http://python-3-patterns-idioms-test.readthedocs.io/en/latest/FunctionObjects.html#command-choosing-the-operation-at-runtime). – Maurice Meyer Jan 12 '17 at 17:28
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.

Nilesh
  • 20,521
  • 16
  • 92
  • 148