Topics

Introduction to Error Handling

Welcome to another tutorial, here you will about Python Error and In-built Exception in Python. In coding, any little mistake can lead to an error in any programming language because the syntax rules must be followed.

 

Python: Syntax Error

Syntax Error is the most common and basic error situation where you break any syntax rule such as if you are working with Python 3.x version. The syntax below is used to print any statement.

print "I love Python!"

Output:

SyntaxError: Missing parentheses in call to 'print'.

It is because for Python 3.x and above the syntax used for the print statement has changed. In addition, if you forget to add a colon (:) at the end of the if condition, a SyntaxError will be returned.

 

if 7 > 5
    print("True")

Output:

SyntaxError: invalid syntax

Thus, when coding in python, syntax errors are the most basic type of errors that are encountered. However, they can easily be fixed by seeing the error message and correcting the code concerning python syntax.

 

Python: What is an Exception?

In Python the exception is a type of error that results from malfunctioning of the coding during execution, it is contrary to a syntax error. 

However, your code might not have any syntax error, still, it can lead to exceptions when it is executed. Check out the example below on dividing a number by zero.

a = 10
b = 0
print("Result of Division: " + str(a/b))

Output:

Traceback (most recent call last): 
  File "main.py", line 3, in <module>
    print("Result of Division: " + str(a/b))
ZeroDivisionError: division by zero

From the result above, we got ZeroDivisonError, but the syntax of the code was very much correct because in this case the error or we should say the exception was generated while coding execution.

The unique thing about Python is that it returns a very detailed exception message for us, so we can understand the origin 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 implies that python has traced back the code to the point from where the exception occurred and also will be showing the related messages after this line.

The second line in the exception message, as shown above, informs us of the name of the python file and the exact line number for the code due to which the exception was generated.

Now, if the second line is not enough, in the third line of the exception message the complete code statement which leads to the exception is printed.

Finally, in the last line, python tells you which exception/error occurred which in the example above is ZeroDivisionError.

 

In-built Python Exception classes

The table below will show some exception classes along with common reasons for their occurrence for future reference.

NOTE: We are calling them exception classes as all these are defined in python as classes.

Exception classDescription
AttributeErrorThis occurs when the attribute that we are trying to access(whether for assigning a value or getting a value) doesn't exist. E.g. Trying to access a class member variable that is not defined in a class.
ImportErrorIt occurs if the imported module is not found.
IndentationErrorIt occurs when there is some issue with the code indentation.
TypeErrorThis occurs when an operation is executed on a variable of the incorrect type.
ValueErrorThis occurs when for a function, the argument value is incorrect.
ZeroDivisionErrorJust as said above, when we try to divide a number with zero.
TabErrorIn this case, the indentation is not consistent throughout the code in terms of tabs and spaces used for indentation.
RuntimeErrorIn this case, an error is not of any specifically defined exception type, python calls it RuntimeError.
NameErrorIn this case, when a variable name we are trying to use is not defined.

These are the few of the built-in exception classes which are most commonly encountered while coding in python.