Table of Contents
Exceptions occur when certain exceptional situations occur in your program. For example, what if you are going to read a file and the file does not exist? Or what if you accidentally deleted it when the program was running? 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 an error handler
for this error does.