Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
13.7. Exceptions Syntax¶
There are many variations on the code that catches exceptions. Here is a brief summary, but other code variations are possible.
13.7.1. Catch All Exceptions¶
Catch all exceptions, regardless of their type. This will prevent your program from crashing, but this type of exception handling is rarely useful because you can’t do anything meaningful to recover from the abnormal condition.
try: # Your normal code goes here. # Your code should include function calls which might raise exceptions. except: # If any exception was raised, then execute this code block.
13.7.2. Catch A Specific Exception¶
This is perhaps the most often used syntax. It catches one specific condition and tries to re-cover from the condition.
try: # Your normal code goes here. # Your code should include function calls which might raise exceptions. except ExceptionName: # If ExceptionName was raised, then execute this block.
13.7.3. Catch Multiple Specific Exceptions¶
try: # Your normal code goes here. # Your code should include function calls which might raise exceptions. except Exception_one: # If Exception_one was raised, then execute this block. except Exception_two: # If Exception_two was raised, then execute this block. else: # If there was no exception then execute this block.
13.7.4. Clean-up After Exceptions¶
If you have code that you want to be executed even if exceptions occur, you
can include a
finally code block:
try: # Your normal code goes here. # Your code might include function calls which might raise exceptions. # If an exception is raised, some of these statements might not be executed. finally: # This block of code will always execute, even if there are exceptions raised
13.7.5. An Example of File I/O¶
One place where you will always want to include exception handling is when
you read or write to a file. Here is a typical example of file processing.
Note that the outer
try: except: block takes care of a missing file or
the fact that the existing file can’t be opened for writing. The inner
try: except: block protects against output errors, such as trying to
write to a device that is full. The
finally code guarantees that the
file is closed properly, even if there are errors during writing.
try: f = open("my_file.txt", "w") try: f.write("Writing some data to the file") finally: f.close() except IOError: print "Error: my_file.txt does not exist or it can't be opened for output."