LEARN PYTHON NOW! Book: The Pillars of Python. 17 — Capture errors

Ruben Ruiz
AI experiments
Published in
3 min readAug 11, 2018

“Happiness isn’t the absence of problems; it’s the ability to deal with them.” Steve Maraboli.

Today we will talk about how capturing mistakes is Python. This is especially useful when working with users. For example, let’s say we ask a user to enter two numbers and then add them together. That’s why we do this simple function:

def sumaf(a,b): 
return(a+b)
print (sumaf("dog",5)# Error
print ("hello")

Our user has kindly tried to add dog with 5. This code will give an error, because if we remember a few chapters ago when we were talking about string types, we said that we couldn’t add a word with a number which makes sense.

Therefore, the above code will launch an error and the interpreter will not continue to run the rest of the lines. To fix and control this type of error we can use what is known as error handling with the try and except commands.

def sumaf(a,b):
try:
return a+b
except:
print("There's a error" )
print (sumaf("dog",5))
print("hello")

Now that error has been “captured”, and the interpreter will continue reading the code until the next line is reached where hello will be displayed.

In addition, if we want to give feedback to the user we could “capture” the exception by using the Exception command and saving it in e :

def sumaf(a,b):
try:
return a+b
except Exception as e:
print("There's a error" )
print (sumaf("dog",5))
print("hello")

There is another term that can help us with our error handling and that is finally’, which will run at the end of the day no matter what happens. This parameter is useful when you need to close a file or the database.

def sumaf(a,b):
try:
return a+b
except Exception as e:
print("There's a error" )
finally:
print("Thank you")
print (sumaf("dog",5))
print("hello")

Finally, we can capture errors if we also know their type of error instead of Exception.

def sumaf(a,b):
try:
return a+b
except TypeError:
print("There's a error" )
finally:
print("Thank you")
print (sumaf("dog",5))
print("hello")

To know the possible types of errors we only have to see the message that the terminal returns to us on the screen:

So far we have seen the most important thing about error handling, which is good to know and is especially useful when we are making more complex programs.

— — — — — — — — — — — — — — The End — — — — — — — — — — — — —

If you like this small and free magazine you can help us by simply sharing it or subscribing to the publication. My name is Rubén Ruiz and I work in Artificial Intelligence in the financial industry and as a personal project I run this little magazine where we experiment with Artificial Intelligence… until the computer explodes :) You can follow me on:

Instagram (Personal life, it’s fun) => @rubenruiz_t

Youtube (Channel about AI, try to make it fun )=> Rubén Ruiz A.I.

Github (Where I upload my code, this is not so much fun anymore) => RubenRuizT

--

--