ERROR TRAPPING IN PYTHON

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 ...

Get Perl To Python Migration now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.