Python Error and In-built Exception in Python
A small typing mistake can lead to an error in any programming language because we must follow the syntax rules while coding in any programming language.
Same is the case with python, in this tutorial we will learn about syntax errors and exceptions in python along with listing down some of the commonly occuring Exceptions in python.
Python: Syntax Error
This is the most common and basic error situation where you break any syntax rule like if you are working with Python 3.x version and you write the following code for printing any statement,
print "I love Python!"
SyntaxError: Missing parentheses in call to ‘print’.
Because, Python 3 onwards the syntax for using the
:) at the end of the
if condition, you will get a SyntaxError:
if 7 > 5
SyntaxError: invalid syntax
Hence syntax errors are the most basic type of errors that you will encounter while coding in python and these can easily be fixed by seeing the error message and correcting the code as per python syntax.
Python: What is an Exception?
Contrary to syntax error, exception is a type of error which is caused as a result of malfunctioning of the code during execution.
Your code might not have any syntax error, still it can lead to exceptions when it is executed.
Let’s take the most basic example of dividing a number by zero:
a = 10
b = 0
print("Result of Division: " + str(a/b))
Traceback (most recent call last): File “main.py”, line 3, in <module> print(“Result of Division: ” + str(a/b)) ZeroDivisionError: division by zero
As we can see in the output, we got ZeroDivisionError while the syntax of our python code was absolutely correct, because in this case the error or we should say the exception was generated while code execution.
Python returns a very detailed exception message for us to understand the origin point and reason for the exception so that it becomes easier for us to fix our code.
Decoding the Exception Message in Python
The term Traceback in the exception message means that python has traced back the code to the point from where the exception occured and will be showing the related messages after this line.
The second line in the exception message, as you can see above, tells us the name of the python file and the exact line number for the code due to which exception was generated.
If that is still not helpful for someone, in the third line of exception message the complete code statement which lead to the exception is printed.
And then in the last line, python tells us which exception/error occured, which in our example above is ZeroDivisionError.
In-built Python Exception classes
Let’s learn about a few exception classes along with common reasons for their occurence for our future reference.
NOTE: We are calling them exception class as all these are defined in python as classes.
|This exception occurs when the attribute that we are tryinh to access(whether for assigning a value or getting a value) doesn’t exists. For example: Trying to access a class member variable which is not defined in class.
|This exception occurs when the imported module is not found.
|This exception occurs when there is some issue with the code indentation.
|When an operation is executed on a variable of incorrect type.
|When for a function, argument value is incorrect.
|As discussed above, when we try to divide a number with zero.
|When the indentation is not consistenet throughout the code in terms of tabs and spaces used for indentation.
|When an error is not of any specific defined exception type, python calls it RuntimeError.
|When a variable name we are trying to use is not defined.
These were some of the built-in excpetion classes which are most commonly encountered while coding in python.