A minor typing error can result in an error in any programming language as we must follow the syntax rules when coding in any programming language.
We can make mistakes when writing a program that causes errors when we run it. When a Python program encounters an unhandled error, it crashes. An exception is a Python object that represents an error. The various types of Python errors are broadly classified as follows:
This is the most common and basic type of error scenario. The most common cause of an error in a Python program is the incorrect use of a statement. A syntax error, also known as a parsing error, is a type of error like this. The Python interpreter reports it quickly, usually with an explanation.
Example:
print"Hello world"
Output:
File "<string>", line 1
print"Hello world"
^
SyntaxError: invalid syntax
Example:
a = 10
if a < 20
print('Value of a less than 20')
Output:
File "<string>", line 3
if a < 20
^
SyntaxError: invalid syntax
In contrast to syntax errors, exceptions are types of errors that occur as a result of code malfunctioning during execution. Exceptions are errors that occur during execution. In Python, all the exception classes are derived from the BaseException class.
Example:
print(5/0)
Output:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ZeroDivisionError: division by zero
It can be seen that dividing five by zero results in ZeroDivisionError, which is a child of the ArithmeticError class.
Now that we understand what an exception is, let’s look at a list of Python exceptions. Try to recall if you’ve ever encountered any of these Python exceptions as you read the list.
Exception | Cause of Error |
AssertionError | When an assert statement fails, an error is raised. |
ArithmeticError | It specifies when a numerical calculation error occurs. |
AttributeError | This exception is thrown when an attribute assignment or reference fails. |
EOFError | When the input() method comes across an “end of file” situation, it throws an error (EOF) |
FloatingPointError | When a floating-point calculation fails, this error is generated. |
GeneratorExit | When the close() method of a generator is called, this variable is raised. |
ImportError | When the imported module cannot be found, this exception is thrown. |
IndexError | When the index of a sequence is out of range, this value is raised. |
KeyError | When a key is not found in a dictionary, this error is raised. |
KeyboardInterrupt | When the user pushes Ctrl+C, Ctrl+Z, or Delete, this exception is thrown. |
MemoryError | When a program runs out of memory, an error is generated. |
NameError | When a variable is not discovered in the local or global scope, this exception is raised. |
NotImplementedError | Raised through abstract methods. |
OSError | When a system operation results in a system-related error, this flag is raised. |
OverflowError | When the result of a numerical calculation is too huge, an error is raised. |
ReferenceError | This Is an Exception When a weak reference object does not exist, an error is raised. |
RuntimeError | When an error does not fit into any of the other categories, it is raised. |
StopIteration | Raised by the next() function to indicate that the iterator has no more items to return. |
SyntaxError | When a syntax problem occurs, the parser raises this exception. |
IndentationError | It raises an error when the indentation is incorrect. |
TabError | It raises an error when the indentation consists of tabs or spaces. |
SystemError | It is triggered when a system error occurs. |
SystemExit | The sys.exit() function raised this exception. |
TypeError | When a function or operation is applied to an object of the wrong type, this exception is raised. |
UnboundLocalError | When a reference to a local variable in a function or method is made but no value is bound to that variable, an exception is raised. |
UnicodeError | When a Unicode-related encoding or decoding problem occurs, this flag is raised. |
UnicodeEncodeError | When a Unicode-related problem occurs during encoding, this flag is raised. |
UnicodeDecodeError | When a Unicode-related error occurs while decoding, this flag is raised. |
UnicodeTranslateError | It defines an error. When there is an issue with a Unicode translation, this flag is raised. |
ValueError | When there is an incorrect value in a specified data type, this exception is raised. |
ZeroDivisionError | When the second operator in a division is zero, an error is raised. |
If necessary, we can even define our own exceptions in Python. We can manage these built-in and user-defined exceptions in Python using try, except, and finally statements.
Note: also read about Static Keyword in Python
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…