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.
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.
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.
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.
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.
a=3; if(a==3): print("Hello from TUF");
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.
To print all the built-in exceptions in python using the following code
This returns a dictionary of built-in exceptions, functions, and attributes.
Some of the built-in exceptions are listed as follows:
|IndentationError||Occurs when proper indentation is missing in the code.|
|ZeroDivisionError||Occurs when division with zero is performed.|
|KeyError||Occurs when a key is not found in the dictionary.|
|ImportError||Occurs when the imported module is not found.|
|MemoryError||Occurs when an operation runs out of memory.|
|FloatingPointError||Occurs when a floating point operation fails to execute.|
|NameError||Occurs when a variable is used which doesn’t exist or is declared.|
|SystemError||Occurs when the interpreter detects an internal error.|
|GeneratorExit||Occurs when the python generator’s closed() method is invoked.|
|NotImplementedError||Occurs because of abstract methods.|
We can also create exceptions. Let’s see how we can do it.
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,
class myException(Exception): pass raise myException("This is a user-defined exception");
We can raise this exception using the raise statement and can also define an error message.
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 article. If you want to suggest any improvement/correction in this article please mail us at [email protected]