What are the Types of Errors in Python?

Video Tutorial
FREE
Intro to Errors & Exception in Python thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

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:

  1. Syntax errors
  2. 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.

ExceptionDescription
IndexErrorAs the name suggests, the IndexError is raised when the wrong index of a list is used.
AssertionErrorAssertionError is raised when the assert statement fails
AttributeErrorAttributeError is raised when an attribute assignment is failed.
ImportErrorImportError is raised when an imported module is not found or there is a problem in importing the required module.
KeyErrorKeyError is raised when the key of the dictionary is not found.
NameErrorNameError is raised when the variable is not yet defined.
MemoryErrorMemoryError is raised when a program runs out of memory.
TypeErrorTypeError is raised when a function or an operation is applied in an incorrect type.
EOFErrorEOFError is raised when the input() function hits the condition of end-of-file.
FloatingPointErrorFloatingPointError is raised when a floating-point operation fails.
GeneratorExitTypeError is raised when we call the generator's close() method.
KeyboardInterruptKeyboardInterrupt is raised when a user hits the interrupt key i.e. Ctrl+C or Delete.
NotImplementedErrorNotImplementedError is raised by abstract methods when they are defined.
OSErrorOSError is raised when system operation causes system-related errors.
OverflowErrorOverflowError is raised when the result of an arithmetic operation is too large.
ReferenceErrorReferenceError is raised when a weak reference proxy is used to access a garbage collected referent.
RuntimeErrorRuntimeError is raised when an error does not fall under any other category of built-in Python exceptions.
StopIterationStopIteration is raised by the iterator's next() function to indicate that there is no further item to be returned by the iterator.
IndentationErrorIndentationError is raised when there is incorrect indentation.
TabErrorTabError is raised when interpreter detects internal error.
SystemErrorSystemError is raised when indentation consists of inconsistent tabs and spaces.
UnboundLocalErrorUnboundLocalError is raised when a reference is made to a local variable in a function or method, having no value bound to that variable.
UnicodeErrorUnicodeError is raised when a Unicode-related encoding or decoding error occurs.
UnicodeEncodeErrorUnicodeEncodeError is raised when a Unicode-related error occurs at the time of encoding.
UnicodeDecodeErrorUnicodeDecodeError is raised when a Unicode-related error occurs at the time of decoding.
UnicodeTranslateErrorUnicodeTranslateError is raised when a Unicode-related error occurs during translating.
ValueErrorValueError is raised when a function is given the correct type of argument but with an improper value.
ZeroDivisionErrorZeroDivisionError 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.