Erle Robotics Learning Python GitBook Free

Errors and Exceptions

Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal.It is possible to write programs that handle selected exceptions.

We use tryand exceptto avoid this errors. Let's see an example:

>>> num=4
>>> if num<10:
...  try:
...   num=num**3
...   print num
...  except:
...   print "Error"
...
64
>>>
>>> num = "Hola"
>>>
>>> if num<10:
...  try:
...   num=num**3
...   print num
...  except:
...   print "Error"
...
Error

If tryis executed successfully the exceptis ignored. If tryfails, the exceptis executed.

Practice 1

Write a program that ask for an integer number. while True use tryto ask this number and then break if the value is not correct, print an error message.

The code syntaxis is the following. Copy this in pra.pyfile:

while True:
 try:
  num=int(raw_input("Enter an integer number:")

  break
 except ValueError:
  print"That is not a valid number!Try again..."

The execution result on this:

 root@erlerobot:~/Python_files# python pra.py
 Enter an integer number:9.8
 That is not a valid number!Try again...
 Enter an integer number:9
 root@erlerobot:~/Python_files#