The Exception Handling document in Junos PyEZ walks you through handling Python exceptions when things go wrong.