in , ,

Python Errors and Built-in Exceptions

Python Errors and Built-in Exceptions

Python Errors and Built-in Exceptions

In this tutorial, you will find out about various sorts of errors and exceptions that are built-in Python. They are raised at whatever point the Python interpreter encounters errors.

We can make certain mistakes while writing a program that leads to blunders when we attempt to run it. A python program ends when it experiences an unhandled mistake. These mistakes can be extensively characterized into two classes:

1. Syntax errors

2. Logical errors (Exceptions)

Python Syntax Errors

Mistake brought about by not following the correct structure (linguistic structure) of the language is called punctuation blunder or parsing error.

Let’s look at one example:

>>> if a < 3
  File "<interactive input>", line 1
    if a < 3
SyntaxError: invalid syntax

As shown in the example, an arrow indicates where the parser ran into the syntax error.

We can notice here that a colon: is missing in the if statement.

Python Logical Errors (Exceptions)

Errors that happen at runtime (in the wake of breezing through the linguistic structure assessment) are called exceptions or logical errors.

For example, they happen when we attempt to open a file(for perusing) that doesn’t exist (FileNotFoundError), attempt to separate a number by zero (ZeroDivisionError) or attempt to import a module that doesn’t exist (ImportError).

Whenever these sorts of runtime errors happen, Python makes an exception object. If not taken care of appropriately, it prints a traceback to that blunder alongside certain insights regarding why that error occurred.

Let’s look at how Python treats these errors:

>>> 1 / 0
Traceback (most recent call last):
 File "<string>", line 301, in runcode
 File "<interactive input>", line 1, in <module>
ZeroDivisionError: division by zero

>>> open("imaginary.txt")
Traceback (most recent call last):
 File "<string>", line 301, in runcode
 File "<interactive input>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'imaginary.txt'

Python Built-in Exceptions

Illegal operations can raise special cases. There are plenty of built-in exceptions in Python that are raised while comparing blunders happen. We can see all the inherent special cases using the built-in local() function as follows:


locals()[‘__builtins__’] will return a module of built-in exceptions, functions, and attributes. dir allows us to list these attributes as strings.

Some of the common built-in exceptions in Python programming along with the error that causes them are listed below:

ExceptionCause of Error
AssertionErrorRaised when an assert statement fails.
AttributeErrorRaised when attribute assignment or reference fails.
EOFErrorRaised when the input() function hits end-of-file condition.
FloatingPointErrorRaised when a floating-point operation fails.
GeneratorExitRaise when a generator’s close() method is called.
ImportErrorRaised when the imported module is not found.
IndexErrorRaised when the index of a sequence is out of range.
KeyErrorRaised when a key is not found in a dictionary.
KeyboardInterruptRaised when the user hits the interrupt key (Ctrl+C or Delete).
MemoryErrorRaised when an operation runs out of memory.
NameErrorRaised when a variable is not found in the local or global scope.
NotImplementedErrorRaised by abstract methods.
OSErrorRaised when system operation causes the system-related error.
OverflowErrorRaised when the result of an arithmetic operation is too large to be represented.
ReferenceErrorRaised when a weak reference proxy is used to access a garbage collected referent.
RuntimeErrorRaised when an error does not fall under any other category.
StopIterationRaised by next() function to indicate that there is no further item to be returned by the iterator.
SyntaxErrorRaised by the parser when a syntax error is encountered.
IndentationErrorRaised when there is incorrect indentation.
TabErrorRaised when indentation consists of inconsistent tabs and spaces.
SystemErrorRaised when the interpreter detects the internal error.
SystemExitRaised by sys.exit() function.
TypeErrorRaised when a function or operation is applied to an object of the incorrect type.
UnboundLocalErrorRaised when a reference is made to a local variable in a function or method, but no value has been bound to that variable.
UnicodeErrorRaised when a Unicode-related encoding or decoding error occurs.
UnicodeEncodeErrorRaised when a Unicode-related error occurs during encoding.
UnicodeDecodeErrorRaised when a Unicode-related error occurs during decoding.
UnicodeTranslateErrorRaised when a Unicode-related error occurs during translating.
ValueErrorRaised when a function gets an argument of correct type but improper value.
ZeroDivisionErrorRaised when the second operand of division or modulo operation is zero.

If required, we can also define our own exceptions in Python. To learn more about them, visit Python User-defined Exceptions.

We can handle these built-in and user-defined exceptions in Python using try, except and finally statements. To learn more about them, visit Python try, except and finally statements.

Please feel free to give your comment if you face any difficulty here.

For more Articles click on the below link.

salman khan

Written by worldofitech

Leave a Reply

Python Directory and Files Management

Python Directory and Files Management

Python Exception Handling Using try, except and finally statement

Python Exception Handling Using try, except and finally statement