0
try :
    doSomething1()
    doSomething2()
    doSomething3()
except Exception as e :
    doSomething4()
}  

I know that in the above code If doSomething1() fails doSomething2() & doSomething3() won't execute and it would jump to doSomething4().
But I want something where if either on of doSomething1() or doSomething2() or doSomething2() fails the whole try doesn't gets executed and jumps to doSomething4()
Actually I'm working with a database and in try I create two rows but sometimes second row fails and first one doesn't, But I don't want the 1st row to be saved when the second one fails. I want both of them to execute or fail together. How?

rook
  • 5,880
  • 4
  • 39
  • 51
pavitran
  • 814
  • 1
  • 10
  • 25
  • 6
    If you work on a database, can't you use a transaction and rollback on error? – Lucero Jan 08 '17 at 15:33
  • 1
    Yes, it sounds like you need transactional behaviour. You should do this using the tools available in your database driver, not with naive Python error handling. – jonrsharpe Jan 08 '17 at 15:35
  • 1
    Python can't go back in time. Once something is done, it can't be undone automatically. – Barmar Jan 08 '17 at 15:37
  • I'm working with Django. Do you know how can I accomplish `transaction and rollback` in Django? – pavitran Jan 08 '17 at 15:37
  • 2
    @pavitran Have you tried [reading the docs](https://docs.djangoproject.com/en/1.10/topics/db/transactions/)? – Tagc Jan 08 '17 at 15:38
  • @Tagc No I haven't yet, I will check it out thanks for the help Lucero and Tagc ! – pavitran Jan 08 '17 at 15:48
  • `try: with transaction.atomic(): # ... if mal != '': raise IntegrityError except IntegrityError: handle_exception()` @Tagc this is what I'm looking for? – pavitran Jan 08 '17 at 16:03
  • @pavitran I don't know; I don't work with Django. Try it out, and if it doesn't work you can try raising a new question here to address the specific problem(s) you encounter. – Tagc Jan 08 '17 at 16:05

1 Answers1

2

You want that if any of the doSomething1, doSomething2 and doSomething3 method fails, then the try block is skipped, and the doSomething4 method is run.

Well, it's not logically possible. If doSomething1 fails, then it has been executed, hence the whole try can no more be entirely skipped. Same for doSomething2: if it fails, then it has been run, and doSomething1 did not failed, so only doSomething3 can still be skipped.

Here are a few different behaviours you can implement.

Behaviour 1

try:
    doSomething1()
    doSomething2()
    doSomething3()
except:
    doSomething4()

Tries to run doSomething1, doSomething2 and doSomething3. If one of them fails, the following ones are skipped, and doSomething4 is executed. Else, doSomething4 is skipped.

Behaviour 2

try:
    doSomething1()
    doSomething2()
    doSomething3()
finally:
    doSomething4()

Tries to run doSomething1, doSomething2 and doSomething3. If one of them fails, the following ones are skipped. In any case, doSomething4 is executed at the end.

Behaviour 3

try:
    doSomething1()
except:
    try:
        doSomething2()
    except:
        pass
    else:
        try:
            doSomething3()
        except:
            pass
        else:
            doSomething4()
else:
    try:
        doSomething2()
    except:
        try:
            doSomething3()
        except:
            pass
        else:
            doSomething4()
    else:
        try:
            doSomething3()
        except:
            doSomething4()

If exactly one of doSomething1, doSomething2 and doSomething3 fails, doSomething4 is run. This behaviour could be implemented in a much easier way with neater exception management, but this code has the merit of using only trys, excepts and elses.

Behaviour 4

try:
    doSomething1()
    doSomething2()
    doSomething3()
    doSomething4()
except:
    ...

This is what corresponds to the title of your question. If none of doSomething1, doSomething2 and doSomething3 fails, doSomething4 is run; else, it is not run.

Right leg
  • 16,080
  • 7
  • 48
  • 81