Common python exception and how to understand them.

 

 

Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991. It is currently being used in a wide range of applications, with the most common applications in data processing and machine learning.

giphy

If you are new to the programming world, then python is the go to language to get your feet wet. But just like any other language (programming or other wise), you will eventually run into some embarrassing situations where what you said, does not specifically imply what you mean.

Thankfully python has a handy system called as exceptions which will help you understand why the computer was not able to understand you. Your first encounter with python exceptions can be a pretty difficult experience, as you don't know how to read a python exception, and fix the problem yourself.

giphy

Luckily we are here to help. This blog will take you through the structure of the python exception and give you tips on how to understand some of the most common exception and how to solve them. You will be able to use the knowledge that you gained from this blog to understand and solve other exceptions yourself.

giphy

 

When an "uncaught" python exception is raised, python prints what kind of exception took place, what exactly happened, and where in the code the issue occurred.

To see an example of what a python exception looks like, go to your python console and type out '1/0' and press enter. You could look at the image below for the same output.

Screenshot 2019-08-23 at 12.28.01 PM

As you can see, the first 2 line are the traceback, which tells you that the error was raised from the input.

The second line tells you 2 things. The first is that the error is of the type 'Zero Division'. Which probably means the code reached a division calculation, where the denominator was zero. The second thing the exception tells is exactly what happened. This second data will make more sense for the following examples.

 

Let's try out a few of the other exceptions which are common to python programming.

Syntax Error:

The syntax error is the exception that python raises, if you did not follow the rule of coding in python which should never be broken. 

giphy

 

Let's do something that raises this exception. Type out the following code in a python file and run it.

data=1.1.2

Anyone who knows a bit of the basics of programming in python would know this is wrong. The issue is that the code is trying to store a float with 2 decimal points in it inside a variable. Python sees this as non-sense and throws the 'syntax error' exception. Checkout the image below for the exception.

 Screenshot 2019-08-23 at 12.37.29 PM

 
As you can see, the trace tells you that the issue occurred in the file named 'test.py' because I named my file 'test.py' and it points to the character '2' with the '^' sign, because it did not expect another integer to follow a '.'.
 
The error shown is 'SyntaxError:' and the exact reason given is 'invalid syntax'. To correct this issue, just go to the line 1 and set the value correctly, and presto the code will start working again.

Name Error:

A name error is raised, when python knows that you are referring to something, but it does not know who you are referring to. Think of it as, you telling to a new friend to pick up an old friend from the train station, but you forget that these friends of yours never met.

giphy

Programmers most commonly face issues with name error, when they name a variable or a function in one way but end up referring to it in another way.
Try out the following code as an example
roger = "This is roger"
print(Roger)

Here is what the exception will look like

Screenshot 2019-08-23 at 12.53.29 PM

As you can see from the traceback, the exception took place in the 'test.py' file on line 2 thins time.

The error raised is 'Name Error' and the exact reason is given as "name 'Roger' is not defined'. This means that python does not know what you are referring to when you say 'Roger'.

The right way to solve this issue, give python the correct name to the variable. Here you can fix this issue, by changing 'print(Roger)' to 'print(roger)'.

Type Error:

A type error is raised, when you tell python to use something for a task which it should not be used for. Imagine trying to fit a square block inside a round hole.

giphy

 

Try out the following code as an example

count = 10
printable = "This is the count " + count

You will get an exception like the one below

Screenshot 2019-08-23 at 1.03.15 PM

Here again, the traceback says that the error took place in line number 2 of 'test.py', showing that the error took place when you tried to assign data to the variable 'printable'

The error raised was 'TypeError' and the exact reason is given as "Cannot concatenate 'str' and 'int' objects'

From the exact reason, you understand that what you are trying to do, is add a string ('str') to an integer ('int') which is a no-no as per python.

An easy way to fix such an issue is to type case the int as a string. To do this, you have to use the line as mentioned below

printable = "This is the count " + str(count)

 

Hurry you reached the end of the Blog. While I would have liked to talk about more exceptions that you may face, you should know that python is a vast language with thousands of different types of exceptions, and programmers are adding new once as we speak. It would not be practically possible for me to cover all of them. I do hope my blog has better equipped you with how to understand and then resolve exception in python. Happy coding!

 

 

exception python error

There are no related posts