print
function call. What if we misspelt print
as Print
? Note the capitalization. In this case, Python raises a syntax error.NameError
is raised and also the location where the error was detected is printed. This is what an error handler for this error does.Enter
key. When your computer prompts you for input, instead press [ctrl-d]
on a Mac or [ctrl-z]
with Windows and see what happens. (If you're using Windows and neither option works, you can try [ctrl-c]
in the Command Prompt to generate a KeyboardInterrupt error instead).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.try..except
statement. We basically put our usual statements within the try-block and put all our error handlers in the except-block.exceptions_handle.py
):try
block and then put handlers for the appropriate errors/exceptions in the except
clause/block. The except
clause can handle a single specified error or exception, or a parenthesized list of errors/exceptions. If no names of errors or exceptions are supplied, it will handle all errors and exceptions.except
clause associated with every try
clause. Otherwise, what's the point of having a try block?else
clause associated with a try..except
block. The else
clause is executed if no exception occurs.raise
statement by providing the name of the error/exception and the exception object that is to be thrown.Exception
class.exceptions_raise.py
):ShortInputException
. It has two fields - length
which is the length of the given input, and atleast
which is the minimum length that the program was expecting.except
clause, we mention the class of error which will be stored as
the variable name to hold the corresponding error/exception object. This is analogous to parameters and arguments in a function call. Within this particular except
clause, we use the length
and atleast
fields of the exception object to print an appropriate message to the user.finally
block.exceptions_finally.py
:time.sleep
function so that the program runs slowly (Python is very fast by nature). When the program is still running, press ctrl + c
to interrupt/cancel the program.KeyboardInterrupt
exception is thrown and the program quits. However, before the program exits, the finally clause is executed and the file object is always closed.None
or a variable which is an empty sequence or collection is considered False
by Python. This is why we can use if: f
in the code above.sys.stdout.flush()
after print
so that it prints to the screen immediately.try
block and subsequently releasing the resource in the finally
block is a common pattern. Hence, there is also a with
statement that enables this to be done in a clean manner:exceptions_using_with.py
:open
function with the with
statement - we leave the closing of the file to be done automatically by with open
.with
statement. It fetches the object returned by the open
statement, let's call it "thefile" in this case.thefile.__enter__
function before starting the block of code under it and always calls thefile.__exit__
after finishing the block of code.finally
block should be taken care of automatically by the __exit__
method. This is what helps us to avoid having to use explicit try..finally
statements repeatedly.try..except
and try..finally
statements. We have seen how to create our own exception types and how to raise exceptions as well.