Python

Python Errors and Built-in Exceptions

Hello in this tutorial, we will understand errors and built-in exceptions in python programming.

1. Introduction

1.1 Error

A typing mistake can lead to a fault in the programming language because of the syntax rules while coding is known as an error or popularly known as the syntax error. A syntax error is the most common situation where you break any syntax rule. The snippet below is a good example of a syntax error where we missed to specify the colon at the end of the if condition.

1
2
3
4
# Syntax error
# Reason - Invalid syntax as you have not added colon (:) at the end of if condition.
if 7 > 5:
    print('True')

1.2 Exceptions

An exception is considered as an unusual condition in a program that breaks the program flow due to an interruption. When an exception occurs, the program stops, and the further code is not executed and thus they are also known as the run-time errors. An exception in python is an object that represents the error and Exception is python is known as the base class for all exceptions. The table below lists the built-in exception that usually occurs in python programming.

Exception
ArithmeticErrorRaised when an error occurs in numeric calculations
AssertionErrorRaised when an assert statement fails
AttributeErrorRaised when attribute reference or assignment fails
EOFErrorRaised when the input() method hits an end of file condition
FloatingPointErrorRaised when a floating-point calculation fails
ImportErrorRaised when an imported module does not exist
IndentationErrorRaised when indentation is not correct
IndexErrorRaised when an index of a sequence does not exist
KeyErrorRaised when a key does not exist in a dictionary
MemoryErrorRaised when a program runs out of memory
NameErrorRaised when a variable does not exist
NotImplementedErrorRaised when an abstract method requires an inherited class to override the method
OverflowErrorRaised when the result of a numeric calculation is too large
ReferenceErrorRaised when a weak reference object does not exist
RuntimeErrorRaised when an error occurs that does not belong to any specific expectations
StopIterationRaised when the next() method of an iterator has no further values
SyntaxErrorRaised when a syntax error occurs
TabErrorRaised when indentation consists of tabs or spaces
SystemErrorRaised when a system error occurs
SystemExitRaised when the sys.exit() function is called
TypeErrorRaised when two different types are combined
ValueErrorRaised when there is a wrong value in a specified data type
ZeroDivisionErrorRaised when the second operator in a division is zero

1.3 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Python Errors and Built-in Exceptions

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of your choice.

2.1 SyntaxError

Let us understand this with the help of a code snippet.

SyntaxError

1
2
3
4
5
6
7
# Syntax error
# Reason - From Python3 onwards print statement has changed and expects the parenthesis.
print('Hello world from Python!')
# Syntax error
# Reason - Invalid syntax as you have not added colon (:) at the end of if condition.
if 7 > 5:
    print('True')

If everything goes well the syntax error warnings will be shown in the IDE console.

Console Output

1
2
3
4
5
6
7
8
== syntax error ==
print 'Hello world from Python!'
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Hello world from Python!')?
if 7 > 5
           ^
SyntaxError: invalid syntax

2.2 ZeroDivisionError

Let us understand this with the help of a code snippet.

ZeroDivisionError

1
2
3
4
5
# Python exception example
a = 10
b = 0
c = a / b
print('Result of division = {}'.format(str(c)))

If everything goes well the following error output will be shown in the IDE console.

Console Output

1
2
3
4
Traceback (most recent call last):
  File "jcg-assignment-python-error-and-exceptions.py", line 15, in
    c = a / b
ZeroDivisionError: division by zero

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • An introduction to errors and built-in exceptions in python programming
  • Sample program to understand the errors and built-in exceptions in python programming

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was an example of understanding the errors and built-in exceptions in python programming.

Download
You can download the full source code of this example here: Python Errors and Built-in Exceptions

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button