EXCEPTION HANDLING IN PYTHON
Unlike JavaScript you cannot add a number and a string in Python. However, you can detect an illegal operation using the try/except construct in Python, which is similar to the try/catch construct in languages such as JavaScript and Java.
An example of a try/except block is here:
try: x = 4 y = 'abc' z = x + y except: print 'cannot add incompatible types:', x, y
When you run the preceding code in Python, the print statement in the except code block is executed because the variables x and y have incompatible types.
Earlier in the appendix you also saw that subtracting two strings throws an exception:
>>> 'a' - 'b' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for -: 'str' and 'str'
A simple way to handle this situation is to use a try/except block:
>>> try:
... print('a' -...