Python errors and built-in exceptions

Introduction

Generally, newbie programmers are not familiar with the errors that may occur when they try to run a code that doesn’t abide by the rules of the python programming language. Hence this article will help you get familiar with errors, and exceptions under which we’ll see some built-in exceptions.

Errors

Usually, errors occur when illegal codes are executed by the programmer. Generally, there are 2 types of errors in python.

Let’s discuss them one by one.

Syntax Errors

As the name suggests, when there is a problem in syntax i.e. the syntax used by the programmer doesn’t fit within the rules of python, a syntax error occurs.

This type of error is also known as a parsing error. 

For example,

Code:

Python Code

print(Hello World);

Output:

In the above example, we missed inverted commas which are used within the print statement to print a string, hence the interpreter prints the error and also prints the line, line number, and file number for a faster lookup of the error.

Logical Errors

These types of errors are also known as exceptions. These errors occur when the program is syntactically correct but fails to deliver a logic that abides by the rules of python. 

Some common examples are division by 0, missing proper indentation, performing operations on different data types, etc.

For example,

Code:

Python Code

print(10/0);

Output:

Code:

Python Code

a=3;
if(a==3):
print("Hello from TUF");

Output:

In the above example, we didn’t provide proper indentation after the if statement hence IndentationError occurred.

If you see in both the examples, the message comprises two types – Exception name and its meaning.

Now let’s see some built-in exceptions within python.

Built-in Exceptions

To print all the built-in exceptions in python using the following code

print(dir(locals()['__builtins__']))

This returns a dictionary of built-in exceptions, functions, and attributes.

Some of the built-in exceptions are listed as follows:

ExceptionsDescription
IndentationErrorOccurs when proper indentation is missing in the code.
ZeroDivisionErrorOccurs when division with zero is performed.
KeyErrorOccurs when a key is not found in the dictionary.
ImportErrorOccurs when the imported module is not found.
MemoryErrorOccurs when an operation runs out of memory.
FloatingPointErrorOccurs when a floating point operation fails to execute.
NameErrorOccurs when a variable is used which doesn’t exist or is declared.
SystemErrorOccurs when the interpreter detects an internal error.
GeneratorExitOccurs when the python generator’s closed() method is invoked.
NotImplementedErrorOccurs because of abstract methods.

We can also create exceptions. Let’s see how we can do it.

User-defined Exceptions

To create user-defined exceptions we need to create a new class that is to be derived from the built-in Exception class. For example,

Code:

Python Code

class myException(Exception):
    pass

raise myException("This is a user-defined exception");

Output:

We can raise this exception using the raise statement and can also define an error message.

Conclusion

Errors play a crucial role in helping the programmer to build a program without any problems and keeping in mind the rules of the python programming language.

When these exceptions occur, the python interpreter stops the current execution and passes it to the code which called it. If these exceptions are not handled properly, it would cause an unexpected termination of the program. Hence we need to handle these exceptions properly which comes under python exception handling which would be covered in another article.

Special thanks to Yash Mishra for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this articleIf you want to suggest any improvement/correction in this article please mail us at [email protected]