Python/Try Statement


Objective

Books-aj.svg aj ashton 01f.png
  • Learn about errors and exceptions.
  • Learn how error handling differs from C.
  • Understand why errors and exceptions never pass silently.
  • Learn about the try, except, else, and finally statements.
  • Read a list of built-in exceptions.

Page Index

Lesson

Errors and Exceptions

In a perfect world, nothing goes wrong. Unfortunately, we don't live in a perfect world, so bad things are bound to happen. Computers can crash, monitors can short, and hard drives can corrupt. In the event that something goes wrong, purposely or not, the computer needs to be able to understand that something is going wrong and stop/fix the problem. Software also needs to be able to handle these problems, too.

Errors happen when something doesn't go right. For example, pulling a CD out of a computer while burning something on it will result in the CD not being completely finished (corrupted) and it will also make the CD unavailable to the software using it (the CD burning program). If the program can't handle this problem it might display undefined behavior, like trying to continue burning to a nonexistent CD or the program could just hang. That's not very helpful to the user, so it's important to take care of these errors. Luckily, Python is able to handle errors and exceptions, unlike other programming languages.

Python Errors vs. C Errors

As we've seen so far in this course, errors are very loud in Python. If something doesn't follow the rules or doesn't go right, Python will make sure you hear about it. Having every little mistake scrutinized without mercy might sound bad at first, but it actually becomes a vital necessity when you learn why. Early programming languages, like C, didn't come with built-in error handling. Sometimes errors went by unnoticed. Other times they caused the computer to crash. It was up to the programmer to create their own error handling support and even then it was still hard to catch errors in their tracks. Python's ultimate error handling goal is to let you know that an error has occurred. Having fulfilled its goal, what happens next is all up to you. If you don't specify anything to happen, then a default error message is displayed and the program is ended.

Loud Errors

Now that we have a bigger picture on errors, it becomes clear that handling errors is important. Imagine creating a large program. The said program crashes when you use it. It then becomes really important on finding and fixing the problem. Python lets you know where the error occurred and what caused the error. This simplifies the error fixing process and it allows for rapid development with the knowledge that errors don't go unnoticed. This is one of the many situations where error handling comes in handy.

The Try and Except Statements

Python allows for errors and exceptions to be handled by the program. To do so, you'll need to use both the try and except statements. A minimal example is given below.

>>> try:
...     print(spam)
... except:
...     print("spam isn't defined!")
...
spam isn't defined!


If any code within the try statement causes an error, execution of the code will stop and jump to the except statement. Here it will execute the code and if, for any reason, there's an error within the except statement, you'll get the message During handling of the above exception, another exception occurred. You should be careful not to put any code that could cause an error within the except statement.

Now, you might have noticed that error messages will identify what kind of error happened on the third line. In the case of the earlier example, a NameError occurred. This kind of error happens when a variable's name can't be found, usually because it doesn't exist. Since errors can be very specific, there needs to be an array of different exception names and there are. Python has at least 20 different built-in exceptions so each problem can be addressed.

Now, what happens if we want to catch a specific error like NameError? To do this, we need to have the said error follow after the except statement as shown in the example below.

>>> try:
...     print(spam)
... except NameError:
...     print("spam isn't defined!")
... except:
...     print("An unknown error has occurred!")
...
spam isn't defined!


We can see that the except statement by itself acts as a default error handler. It will handle any errors not caught by another except. This means we can have ten different except statements working to catch errors.

Exceptions

The Else Statement

The else statement functions just like it does with the while and for statements. It executes if the try statement finishes without any errors or premature endings. This statement must come after the group of except statements like the example below demonstrates.

>>> try:
...     spam = 7
...     print("I have %d cans of spam!" % spam)
... except:
...     print("Some error has occurred!")
... else:
...     print("Everything went smoothly!")
...
I have 7 cans of spam!
Everything went smoothly!

The Finally Statement

The finally statement is similar to the else statement, except it will always execute after the try statement, regardless if it fails or ends prematurely. The finally statement must go after the else statement. The example below demonstrates.

>>> try:
...     print("pop")
... except:
...     print("An error has occurred!")
... else:
...     print("Everything went smoothly!")
... finally:
...     print("Finishing up now...")
...
pop
Everything went smoothly!
Finishing up now...
>>> try:
...     print("soda")
... except:
...     print("An error has occurred!")
... finally:
...     print("Cleaning up any mess...")
...
soda
Cleaning up any mess...

Examples


Python has powerful error detection capabilities. If it's possible to produce an error (and it usually is), Python is ready to pounce on it and broadcast to the world if an error has occurred. It's the responsibility of the programmer to anticipate and handle errors so that, despite errors, the job completes successfully or terminates gracefully. We software engineers should be grateful for Python's error detection power; we can spend less time chasing errors and more time writing good code.


We've seen some errors already: SyntaxError TypeError IndexError NameError UnicodeEncodeError ValueError KeyError KeyboardInterrupt ZeroDivisionError. Let's review some of these errors and a few more and create situations in which they are likely to occur.

>>> if a == 6 : print ('a = 6')
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>>
>>> a = 4 ; if a == 6  print ('a = 6')
  File "<stdin>", line 1
    a = 4 ; if a == 6  print ('a = 6')
             ^
SyntaxError: invalid syntax
>>> 
>>> a = '2' + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be str, not int
>>> 
>>> a = [0,1,2,3,4] ; a[5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> 
>>> votes = ["yes", "no", "yes", "yes", "no"]
>>> votes.index("maybe")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'maybe' is not in list
>>>
>>> a = {} ; a['Bill']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'Bill'
>>> 
>>> a = array('B', [1,2,3,4]) ;  a[2] = 0xfff
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: unsigned byte integer is greater than maximum
>>>
>>> 2/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> # The above exits with status = 1.
>>>

Python's try statement is a means which we can use to handle errors gracefully. If the error is handled properly, execution of the code does not produce error status and there is no output to stderr.

try :
    2 / 0
except ZeroDivisionError : 
    print ('Division by 0 detected.') # This printed

The above exits with status = 0 and there is no output to stderr.

try :
    2 / 0
except ZeroDivisionError :
    print ('Division by 0 detected.')
except :

exit (0)
  File "t3.py", line 9
    exit (0)
       ^
IndentationError: expected an indented block
try :
    a =	[1,2,3]	; a[4]
except Indexerror :
    print ('Indexerror detected.')
except :
    print ('Unknown error detected.')
Traceback (most recent call last):
  File "t3.py", line 5, in <module>
    a =[1,2,3]; a[4]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "t3.py", line 6, in <module>
    except Indexerror :
NameError: name 'Indexerror' is not defined

The example above stresses that the code processing errors should not introduce another error.

Correct the spelling mistake and try again:

try :
    a =	[1,2,3]	; a[4]
except IndexError :
    print ('IndexError detected.') # This prints and the code exits with status = 0.
except :
    print ('Unknown error detected.')

The following code tests a selection of different errors, including errors raised by the code and also an error label defined by the user.

import sys

class label(Exception): pass  # declare a label                                                                           

for condition in range(1, 17) :
    print ('\ncondition = {}. '.format(condition), end='')
    try :
        if condition == 1 :     2 / 0
        elif condition == 2 :   a = [1,2,3] ; a[4]
        elif condition == 3 :   b = c
        elif condition == 4 :   a = '2' + 2
        elif condition == 5 :   a = {} ; a['Bill']
        elif condition == 6 :   b = array('B', [1,2,3,4]) ; b[2] = 0xfff
        elif condition == 7 :   raise ZeroDivisionError
        elif condition == 8 :   raise IndexError
        elif condition == 9 :   raise NameError
        elif condition == 10 :  raise TypeError
        elif condition == 11 :  raise KeyError
        elif condition == 12 :  raise OverflowError
        elif condition == 13 :  raise label
        elif condition == 14 :  raise IndentationError
        elif condition == 15 :  raise UnicodeError
    except ZeroDivisionError :  print ('ZeroDivisionError detected.')
    except IndexError :         print ('IndexError detected.')
    except NameError :          print ('NameError detected.')
    except TypeError :          print ('TypeError detected.')
    except KeyError :           print ('KeyError detected.')
    except OverflowError :      print ('OverflowError detected.')
    except label :              print ('Arrived at label.')
    except :
        print ('Unknown error detected:')
        a = sys.exc_info # Info about unknown error that caused exception.                                              
        print ('    ', a)
        b = [ str(p) for p in a ]
        print ('    ', b)
    else :                      print ('Smooth sailing.')

sys.exit (0)
condition = 1. ZeroDivisionError detected.

condition = 2. IndexError detected.

condition = 3. NameError detected.

condition = 4. TypeError detected.

condition = 5. KeyError detected.

condition = 6. NameError detected. # I cannot explain this.

condition = 7. ZeroDivisionError detected.

condition = 8. IndexError detected.

condition = 9. NameError detected.

condition = 10. TypeError detected.

condition = 11. KeyError detected.

condition = 12. OverflowError detected.

condition = 13. Arrived at label.

condition = 14. Unknown error detected:
     (<class 'IndentationError'>, IndentationError, <traceback object at 0x101a43dc8>)
     ["<class 'IndentationError'>", 'None', '<traceback object at 0x101a43dc8>']

condition = 15. Unknown error detected:
     (<class 'UnicodeError'>, UnicodeError, <traceback object at 0x101a50b48>)
     ["<class 'UnicodeError'>", '', '<traceback object at 0x101a50b48>']

condition = 16. Smooth sailing.


Assignments

Crystal Clear app kedit.svg
Nuvola apps kcontrol.gif Completion status: Almost complete, but you can help make it more thorough.

References

  1. ? Python Software Foundation. "Exception hierarchy" (HTML). Built-in Exceptions. Retrieved 2015-05-05. 

1. Python's documentation:

"8. Errors and Exceptions" "8.4. The try statement"


2. Python's methods:


3. Python's built-in functions:

"sys.exc_info," "sys.exit(....)"


  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.


Python/Try_Statement
 

Manage research, learning and skills at IT1me. Create an account using LinkedIn to manage and organize your IT knowledge. IT1me works like a shopping cart for information -- helping you to save, discuss and share.


  Contact Us  |  About IT1me.com |  IT Training & References |  IT Careers |  IT Hardware |  IT Software |  IT Books