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 | |
---|---|
ArithmeticError | Raised when an error occurs in numeric calculations |
AssertionError | Raised when an assert statement fails |
AttributeError | Raised when attribute reference or assignment fails |
EOFError | Raised when the input() method hits an end of file condition |
FloatingPointError | Raised when a floating-point calculation fails |
ImportError | Raised when an imported module does not exist |
IndentationError | Raised when indentation is not correct |
IndexError | Raised when an index of a sequence does not exist |
KeyError | Raised when a key does not exist in a dictionary |
MemoryError | Raised when a program runs out of memory |
NameError | Raised when a variable does not exist |
NotImplementedError | Raised when an abstract method requires an inherited class to override the method |
OverflowError | Raised when the result of a numeric calculation is too large |
ReferenceError | Raised when a weak reference object does not exist |
RuntimeError | Raised when an error occurs that does not belong to any specific expectations |
StopIteration | Raised when the next() method of an iterator has no further values |
SyntaxError | Raised when a syntax error occurs |
TabError | Raised when indentation consists of tabs or spaces |
SystemError | Raised when a system error occurs |
SystemExit | Raised when the sys.exit() function is called |
TypeError | Raised when two different types are combined |
ValueError | Raised when there is a wrong value in a specified data type |
ZeroDivisionError | Raised 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.
You can download the full source code of this example here: Python Errors and Built-in Exceptions