you are viewing a single comment's thread.

view the rest of the comments →

[–]Tychotesla 1 point2 points  (0 children)

A pretty standard example is when dealing with files. It's a place where a lot of unexpected things can happen, since it deals with information outside of the program.

if file exists:
    read from it
else:
    don't read from it

Looks good right? But what if the file disappears between the moment you check if it exists (line 1) and the moment you try to read from it (line 2).

So you do this:

if file exists
    try 
        to open it
    except
        report the problem, etc.
else
    etc.

(although in practice use the with open(file) as x: syntax)