November 2001
Beginner
320 pages
5h 53m
English
When migrating to Python it's important to basically forget everything you know about error trapping in Perl. Python functions don't as a rule return a value that indicates their success or failure. When opening a file for example we don't have to explicitly test the return value of the open function to determine whether the open has succeeded. Instead, Python supports a number of exceptions.
In principle, these appear to work in the same fashion as embedding Perl statements into eval blocks. Exceptions propagate up through the code and they can be trapped and located locally, or “remotely.” For example, below is a function which accepts a single argument, the file name. The function opens the file, reads the lines from ...