In this article, we will discuss about python errors, types of errors and python in-built exception with the help of examples.
Python Errors
Errors refers to the mistakes which we make while writing a program and which stops the execution of that particular program. If a unhandled error occurs in python, then it simultaneously terminates the program.
Types of Python Errors
In python programming language, you will able to see two types of errors which are discussed as follows;
1. Syntax Errors
In python, syntax error refers to those errors which is thrown when a proper structure or syntax of a language is not followed by the programmers. Syntax Error is also called Parsing Error.
# initialize the amount variabledigits = 100
# check that You are eligible to
if(digits>29)
print("Hence Verified!!")
Output
File "<string>", line 4if(digits>29)
^
SyntaxError: invalid syntax
2. Logistic Errors (Exceptions)
Logistic Errors refers to those errors which occurs at the runtime after passing the syntax. These errors also known as Exceptions. When the normal flow of a program change due to internal events, it raises an exception. An exception object is created when these logical type errors occur in a program and python creates a traceback to that error if it is not handled properly. For example - The exception may occur when when we try to import non-existing module i.e., or when we try to open a non-existing file i.e., .
Example 1: Got an error when you perform division with 0
# initialize the amount variableresult = 100
# perform division with 0
x = result / 0
print(x)
Output
Traceback (most recent call last):File "<string>", line 4, in <module>
ZeroDivisionError: division by zero
Example 2: Got an error when indentation is not correct.
if(a<3):
print("Foobar")
Output
File "<string>", line 2print("Foobar")
^
IndentationError: expected an indented block
Built-in Python Exceptions
Here is the list of common exception which occurs in the python programming language;
Exception | Cause of Error |
---|---|
ValueError | It occurs when a function gets correct argument type but improper value. |
TypeError | It occurs when a function is applied to an object of incorrect type. |
NameError | It occurs when the variable is not found in global and local scope. |
ZeroDivisionError | It occurs when the second operation of a division is zero. |
IndexError | When the index of a sequence is out of range, it occurs. |
SyntaxError | It occurs when the syntax error is encountered. |
AttributeError | It occurs when the attribute assignment fails. |
EOFError | It occurs when the input function reach the end-of-file condition. |
RuntimeError | It occurs when an error doesn't fall under other category. |
IndentationError | It is raised by incorrect indentation. |
AssertionError | It occurs when the assert statement fails. |
KeyError | It occurs when the key is not found in the dictionary. |
MemoryError | It occurs when an operation runs out of memory. |
FloatingPointError | When the floating point operation fails, it occur. |
SystemExit | It is raised by the sys.exit() function. |
OSError | It occurs when the system related error is caused by the system operation. |
NotImplementedError | It is raised by the abstract methods. |
OverflowError | It occurs when the arithmetic operation's result is too large to be represented. |
UnicodeError | It is raised when unicode-related decoding or encoding error is occurred. |
UnicodeTranslateError | It is raised when unicode-related error occurred during translating. |
UnboundLocalError | It occurs when a reference is made to a local variable in function but no value has been assigned to that variable. |
ReferenceError | It occurs when to access a garbage collected referent, a weak reference proxy is used. |
UnicodeDecodeError | It occurs when unicode-related error raised during decoding. |
UnicodeEncodeError | It occurs when unicode-related error raised during encoding. |
GeneratorExit | It occurs when the close() method of generator is called. |
ImportError | It occurs when the import module is not found. |
KeyboardInterrupt | It occurs when the interrupt key is hit by the user. |
StopIteration | It occurs when the next() function indicates that there is nothing to be returned by the iterator. |
TabError | It occurs when indentation consists of inconsistent tabs and spaces. |
SystemError | It occurs when the internal error is detected by the interpreter. |
Conclusion
Above we have discussed about python errors, types of errors and python in-built exception with the help of examples. Errors refers to the mistakes which we make while writing a program and which stops the execution of that particular program. In python programming language, you will able to see two types of errors i.e., syntax errors and logistic error or exceptions.