Destination else and finally exception handling

Are the sections handling else and finally redundant exceptions? For example, is there a difference between the following two pieces of code?

 try: foo = open("foo.txt") except IOError: print("error") else: print(foo.read()) finally: print("finished") 

and

 try: foo = open("foo.txt") print(foo.read()) except IOError: print("error") print("finished") 

In general, is it impossible to else content always move in try and can't finally content just move outside the try / catch block? If so, what is the purpose of the else and finally ? Is it just for readability?

+49
python
May 18 '11 at 22:53
source share
4 answers

The idea is that you save the code for which you handle exceptions as little as possible. Everything in the else block can be moved to try , yes, but then you may eventually catch the exception when you want it to be raised. You may have successfully opened the file, but if read raises an IOError and it is in try , it will also be found.

From the horse's mouth :

Using the else clause is better than adding additional code to the try clause, since it avoids accidentally detecting an exception that was not thrown by protected code by the try ... except .

As already mentioned in two other answers, a finally block exists for code that will be executed regardless of whether an exception exists anywhere, including inside else or except , and this exception is not processed. The canonical use case for this is absolutely certain that the file descriptor is closed, no matter what. *

official phrase :

If an exception occurred in the try clause and was not handled by the except clause (or it occurred in the except or else clause), it is raised again after finally . The finally clause is also executed β€œin the output” when any other clause of the try is left with the break , continue or return .




* This particular use has been somewhat eliminated by context managers ( with...as blocks).

+44
May 18 '11 at 10:59 p.m.
source share

finally is executed regardless of whether the statements in the try block fail or fail. else is executed only if no exceptions are thrown in the try block.

+29
May 18 '11 at 22:55
source share

No matter what happens, the block in finally always executed. Even if the exception has not been handled or the exception handlers themselves generate new exceptions.

+8
May 18 '11 at 22:56
source share

If you move the contents of the else block inside the try block, you will also catch exceptions that may occur during the else block. If the line

 print(foo.read()) 

in your example, throws an IOError , your first code snippet will not catch this error, while your second snippet will. You try to keep try blocks as small as possible in general, to really catch only those exceptions that you want to catch.

The finally block is always executed no matter what. If, for example, the try block contains a return , the finally block will still be executed, and any code under the entire try / except block will not.

+6
May 18 '11 at 10:58 p.m.
source share



All Articles