In this article we shall see some of the built in exceptions in python. Just like built in methods these exceptions are built into python. Just like our own exceptions handling python also has some of the built in exceptions. Some of the built in exceptions that will be dealt in this article are,
First let us see what is exception handling in python.
n Python, exceptions can be handled using a
The critical operation which can raise an exception is placed inside the
try clause. The code that handles the exceptions is written in the
We can thus choose what operations to perform once we have caught the exception. Here is a simple example.
while True: try: x = int(input("Please enter a number: ")) break except ValueError: print("Oops! That was no valid number. Try again...")
Now let us see the built in exceptions one by one and the methods to reproduce them with an example code snippet for each of them.
AttributeError can be defined as an error that is raised when an attribute reference or assignment fails. Attribute errors in Python are generally raised when an invalid attribute reference is made.
Suppose we have an integer value assigned to a variable and if we are trying to perform the split method on the variable we will get an attribute error.
The reason for getting this error is because split() method can only be applied to the strings and we are trying to apply this method to an integer.
The ImportError is raised when an import statement has trouble successfully importing the specified module. Typically, such a problem is due to an invalid or incorrect path, which will raise a ModuleNotFoundError in Python 3.6 and newer versions.
The IndexError is one of the more basic and common exceptions found in Python, as it is raised whenever attempting to access an index that is outside the bounds of a list .
Suppose there is a list with only 3 elements and we are trying to retrieve an element from the index 10, we will get index error and also a message saying that “list index out of range”.
A Python KeyError exception is what is raised when you try to access a key that isn’t in a dictionary ( dict ). Python’s official documentation says that the KeyError is raised when a mapping key is accessed and isn’t found in the mapping. … The most common mapping in Python is the dictionary.
I have a dictionary with keys name and age. If i access the value from the dictionary using the key ‘name’ I will get the value of the key name. If I try to access some other value other than that those present in the dictionary i will get a key not found error.
As the name suggest this exception will occur if we quit or program before it finishes normally by (ctrl + c).
Since i have used the sleep method here, the above code will take 5 seconds to terminate. I quit the program by pressing (ctrl + c) before the end of the 5 seconds and hence i got the import error.
A NameError means that Python tried to use a variable or function name, such as hello based on a previous definition. If it hasn’t been defined at this point, you get the error. Usual Causes: A mistyped variable or function name.
I tried to print the variable x without declaring it anywhere before and hence i got NameError.
Syntax errors are the most basic type of error. They arise when the Python parser is unable to understand a line of code. Most syntax errors are typos, incorrect indentation, or incorrect arguments. If you get this error, try looking at your code for any of these.
In python, the if statement usually expects a colon(:) symbol at the end. Since there is no colon at the end of the if statement here the compiler throws a SyntaxError.
Since python makes use of procedural language, if you miss out on adding tabs or spaces between your lines of code, then you will most likely experience this error. Since python does not use curly braces it uses indentation for this purpose.
Python ValueError is raised when a function receives an argument of the correct type but an inappropriate value. Also, the situation should not be described by a more precise exception such as IndexError.
In newer python versions, ValueError is changed as TypeError.
Since the sum() method in python takes list as an input, providing an integer as an input throws TypeError.
If we divide any number by 0 we will get a zero division error.
Thank you for making all the way down here. Hope this article was helpful.