What are the Types of Errors in Python?
Errors are problems that occur in the program due to an illegal operation performed by the user or by the fault of a programmer, which halts the normal flow of the program. Errors are also termed bugs or faults. There are mainly two types of errors in python programming. Let us learn about both types of python errors:
- Syntax errors
- Logical Errors or Exceptions
Whenever we do not write the proper syntax of the Python programming language (or any other language) then the python interpreter throws an error known as a syntax error.
On the other hand, Logical Errors are those errors that cannot be caught during compilation time. As we cannot check these errors during compile time, we name them Exceptions. Exceptions can cause some serious issues so we should handle them effectively.
Note: Syntax errors can also be called Compile Time Error. Some of the most common compile-time errors are syntax errors, library references, incorrect import of library functions and methods, uneven bracket pair(s), etc.
Syntax Errors
A syntax error is one of the most basic types of error in programming. Whenever we do not write the proper syntax of the python programming language (or any other language) then the python interpreter or parser throws an error known as a syntax error. The syntax error simply means that the python parser is unable to understand a line of code.
Let us take an example to understand the syntax error better.
Output:
As we can see in the example above, the python interpretation raised a syntax error saying invalid syntax. Since we have missed the semicolon (:) after the if statement so that's why the python interpreter raised a syntax error.
Some of the general syntax errors can be typing errors (errors), incorrect indentation, or incorrect arguments. In case of syntax errors, we should look at our code and try to understand the problem.
Note: In various IDEs (Integrated Development Environment), the syntax error(s) are shown by red dashed lines.
Logical Errors
As we have seen above there are two main types of python errors. We already have studied the syntax error, let us now learn about the logical errors in python.
Logical Errors are those errors that cannot be caught during compilation time. As we cannot check these errors during compile time, we name them Exceptions. Since we cannot check the logical errors during compilation time, it is difficult to find them.
There is one more name of logical error. Run time errors are those errors that cannot be caught during compilation time. So, run-time can cause some serious issues so we should handle them effectively.
Let us now talk about some of the most common logical types of errors in python programming.
ZeroDivisionError Exception
ZeroDivisionError is raised by the Python interpreter when we try to divide any number by zero. Let us take an example to understand the ZeroDivisionError.
Output:
Indentation is not Correct
The Indentation error is another type of error in python that can be summed up inside the syntax error. As we know, we use indentation to define a block of code in python. So, in case of improper indentation, the Python interpreter raises an Indentation error. Let us take an example to understand the Indentation Error.
Output:
Built-in Exceptions
Like any other programming language, Python has some built-in exceptions.
Note: All instances in Python must be instances of a class that derives from BaseException.
Let us learn about the common built-in exceptions in python.
Exception | Description |
---|---|
IndexError | As the name suggests, the IndexError is raised when the wrong index of a list is used. |
AssertionError | AssertionError is raised when the assert statement fails |
AttributeError | AttributeError is raised when an attribute assignment is failed. |
ImportError | ImportError is raised when an imported module is not found or there is a problem in importing the required module. |
KeyError | KeyError is raised when the key of the dictionary is not found. |
NameError | NameError is raised when the variable is not yet defined. |
MemoryError | MemoryError is raised when a program runs out of memory. |
TypeError | TypeError is raised when a function or an operation is applied in an incorrect type. |
EOFError | EOFError is raised when the input() function hits the condition of end-of-file. |
FloatingPointError | FloatingPointError is raised when a floating-point operation fails. |
GeneratorExit | TypeError is raised when we call the generator's close() method. |
KeyboardInterrupt | KeyboardInterrupt is raised when a user hits the interrupt key i.e. Ctrl+C or Delete. |
NotImplementedError | NotImplementedError is raised by abstract methods when they are defined. |
OSError | OSError is raised when system operation causes system-related errors. |
OverflowError | OverflowError is raised when the result of an arithmetic operation is too large. |
ReferenceError | ReferenceError is raised when a weak reference proxy is used to access a garbage collected referent. |
RuntimeError | RuntimeError is raised when an error does not fall under any other category of built-in Python exceptions. |
StopIteration | StopIteration is raised by the iterator's next() function to indicate that there is no further item to be returned by the iterator. |
IndentationError | IndentationError is raised when there is incorrect indentation. |
TabError | TabError is raised when interpreter detects internal error. |
SystemError | SystemError is raised when indentation consists of inconsistent tabs and spaces. |
UnboundLocalError | UnboundLocalError is raised when a reference is made to a local variable in a function or method, having no value bound to that variable. |
UnicodeError | UnicodeError is raised when a Unicode-related encoding or decoding error occurs. |
UnicodeEncodeError | UnicodeEncodeError is raised when a Unicode-related error occurs at the time of encoding. |
UnicodeDecodeError | UnicodeDecodeError is raised when a Unicode-related error occurs at the time of decoding. |
UnicodeTranslateError | UnicodeTranslateError is raised when a Unicode-related error occurs during translating. |
ValueError | ValueError is raised when a function is given the correct type of argument but with an improper value. |
ZeroDivisionError | ZeroDivisionError is raised when we try to divide any number by zero. |
Learn More
Errors are problems that occur in the program due to an illegal operation performed by the user or by the fault of a programmer. Exception Handling is the process of handling errors and exceptions such that the normal execution of the system is not halted. Exception handling is required to prevent the program from terminating abruptly.
To learn more about exceptions and exception handling, refer to the article - Exception Handling in Python
Conclusion
- Errors are the problems that occur in the program due to an illegal operation performed by the user or by the fault of a programmer, which halts the normal flow of the program.
- Errors are also termed bugs or faults. There are mainly two types of errors in python programming namely - Syntax errors and Logical Errors or Exceptions.
- Whenever we do not write the proper syntax of the python programming language (or any other language) then the python interpreter throws an error known as syntax error.
- Syntax errors can also be called Compile Time Error. Some of the most common compile-time errors are syntax errors, library references, incorrect import of library functions and methods, uneven bracket pair(s), etc.
- Logical Errors are those errors that cannot be caught during compilation time. As we cannot check these errors during compile time, we name them Exceptions.
- Exception Handling is the process of handling errors and exceptions such that the normal execution of the system is not halted.