I follow the Apress, Beginning Python from Novice to Professional book. It is mentioned that:
eventually. You can use try / finally if you need to make sure that some code (such as cleanup code) is executed regardless of whether an exception is raised or not. This code is then placed at the end of the paragraph. Note that you cannot have both except sentences and, finally, in the same try statement, but you can put it inside the other.
I tried this code:
def someFunction(): a = None try: a = 1 / 0 except ZeroDivisionError, e: print 'Yesss' print e finally: print 'Cleanup' del a if __name__ == '__main__': someFunction()
... and conclusion
Yesss integer division or modulo by zero Cleanup
Here I used except and finally in the same try segment, right? And the code is working fine as expected. I can’t understand what the book says!
Someone, please specify. Thanks.
bdhar
source share