Table of Contents
Exceptions occur when certain exceptional situations occur in your program. For example, what if you are reading a file and you accidentally deleted it in another window or some other error occurred? Such situations are handled using exceptions.
What if your program had some invalid statements? This is handled by Python which raises its hands and tells you there is an error.
Consider a simple print statement. What if we misspelt print as Print? Note the capitalization. In this case, Python raises a syntax error.
>>> Print 'Hello, World' File "<stdin>", line 1 Print 'Hello, World' ^ SyntaxError: invalid syntax >>> print 'Hello, World' Hello, World >>>
Observe that a SyntaxError is raised and also the location where the error was detected, is printed. This is what a handler for the error does.