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.
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.
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.
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.
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.
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.
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.
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.
roger = "This is roger"print(Roger)
Here is what the exception will look like
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)'.
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.
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
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!