Errors and exceptions
Consider a simple print function call. What if we misspelt print as Print? Note the capitalization. In this case, Python raises a syntax error.
>>> Print('Hello World') Traceback (most recent call last): File "
", line 1, in Print('Hello World') NameError: name 'Print' is not defined >>> print('Hello World') Hello World
Observe that a NameError is raised and also the location where the error was detected is printed. This is what an error handler for this error does.
We will try to read input from the user. Press ctrl-d and see what happens.
>>> s = input('Enter something --> ') Enter something --> Traceback (most recent call last): File "
", line 1, in s = input('Enter something --> ') EOFError: EOF when reading a line
Python raises an error called EOFError which basically means it found an end of file symbol (which is represented by ctrl-d) when it did not expect to see it.