Programmer errors

Now that we're confident with the control flow for exception behavior, we can remove the print statements:

def convert(s):
"""Convert a string to an integer."""
x = -1
try:
x = int(s)
except (ValueError, TypeError):
return x

But now when we try to import our program:

>>> from exceptional import convert
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "./exceptional.py", line 11
return x
^
IndentationError: expected an indented block

we get yet another type of exception, an IndentationError, because our except block is now empty and empty blocks are not permitted in Python programs.

This is not an exception type that is ever useful to catch with an except block! Almost anything that goes wrong with a Python program results in an exception, but some exception types, such as IndentationError, SyntaxError and NameError, are the result of programmer errors which should be identified and corrected during development rather than handled at runtime. The fact that these things are exceptions is mostly useful if you're creating a Python development tool such as a Python IDE, embedding Python itself in a larger system to support application scripting, or designing a plugin system which dynamically loads code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset