Page 214 - Beginning Programming with Pyth - John Paul Mueller
P. 214

information into an easily read error message.
If you should decide to create the myfile.txt file, the else clause executes. In this case, you see a message stating that the file opened normally. The code then closes the file without doing anything with it.
2. Click Run Cell.
The application displays the Error opening file information, as shown in Figure 10-5.
FIGURE 10-5: Attempting to open a nonexistent file never works.
           import sys
           try:
File = open('myfile.txt') except IOError as e:
for Arg in e.args: print(Arg)
else:
print("File opened as expected.") File.close();
The args
A more complex method of dealing with the issue is to print both the names and the contents of
    OBTAINING A LIST OF EXCEPTION
 ARGUMENTS
 The list of arguments supplied with exceptions varies by exception and by what the sender
 provides. It isn’t always easy to figure out what you can hope to obtain in the way of additional
 information. One way to handle the problem is to simply print everything by using code like this:
 property always contains a list of the exception arguments in string format. You can
  use a simple
for
loop to print each of the arguments. The only problem with this approach is that
 you’re missing the argument names, so you know the output information (which is obvious in
 this case), but you don’t know what to call it.
 










































































   212   213   214   215   216