Errors and Exceptions in Python
No matter how good you make your code, things can go wrong.
When we plan our program, we always work for situations we normally expect.
And our program works quite well in those situations.
But we often ignore the possibility of unexpected situations which can mess with the normal execution of our program.
Let’s learn about such situations and what exactly happens when our program encounters Python error and exception.
Keeping you updated with latest technology trends, Join TechVidvan on Telegram
You might have come across syntax errors millions of times in your initial stages of learning Python.
And even if your syntax is correct, you might encounter errors while execution of your program.
Errors are unusual and unexpected situations that affect the normal flow of program execution.
We make errors all the time.
Some of these errors are “compile-time” errors.
These errors occur before the program execution begins, i.e., at the time of compilation.
While the other errors are “run-time” errors and occur after the code is successfully compiled and is running.
The two distinguishable types of errors in Python are:
- Syntax errors – Compile-time error
- Exceptions – Run-time errors
Let’s have a look at Syntax errors before talking about the ‘elephant in the room’– Exceptions.
Python Syntax Error
A syntax error occurs when you don’t follow proper rules and structure of the language.
A very common mistake that we make while getting accustomed to Python’s syntax is not using correct indentation.
This results in a syntax error.
Another example would be not putting a colon after a function header, or not enclosing a string within quotes.
>>> l = [1, 2, 3] >>> for i in l SyntaxError: invalid syntax >>>
Recognize the error here?
We forgot to put a colon after “for i in l”.
And as we press Enter to start the for-loop’s block, we get a SyntaxError message.
There are so many places in your code where you can possibly go wrong.
And as soon as the compiler recognizes your mistake, it pops up a syntax error message.
Your program will not start executing until you correct all the syntax errors in your code.
Even if you get the syntax right, your program might get stuck somewhere during execution.
This is due to the errors in your code which occur at run-time.
There is no way for your compiler to recognize such errors.
Exceptions in python are the errors that occur during execution.
Well simply put, exceptions occur when your program tries to perform an operation it wasn’t supposed to.
This is when your program crashes and the interpreter displays a runtime exception message on the screen.
There are many different circumstances under which your Python program might crash.
In fact, Python’s interpreter comes up with a complete list of built-in exception types.
Common Python Exceptions
You’ll find some of the common exceptions in the table below.
|Exception||Why it gets raised|
|AssertionError||When an assert statement fails.|
|AttributeError||When an attribute reference or assignment fails.|
|EOFError||When input() function encounters End Of File(EOF).|
|FloatingPointError||When an operation on floating-point fails.|
|ImportError||When a module you want to import is not found.|
|IndexError||When you try to access the element at an index which is out of range.|
|KeyError||When a key is not present in a dictionary.|
|KeyboardInterrupt||When the user presses the interrupt key(Control-c or Delete).|
|MemoryError||When an operation runs out of memory.|
|NameError||When a variable is not found in scope.|
|NotImplementedError||When an abstract method is not implemented anywhere in the code.|
|OSError||When a system-related error occurs.|
|OverflowError||When an arithmetic operation produces a really large result|
|RuntimeError||When a run-time error doesn’t fall in any of the other categories.|
|StopIteration||The next() function raises it when there are no more elements in the iterable.|
|SyntaxError||When there is an error in the syntax.|
|IndentationError||When the indentation is not correct.|
|TabError||Inconsistent tabs and spaces in the indentation. It is a subclass of IndentationError.|
|SystemError||When the interpreter detects an internal error.|
|TypeError||When an operation or function tries to operate on an object of the incorrect type.|
|UnboundLocalError||When you reference a local variable before assigning it a value.|
|ValueError||When you pass an object of correct type but the improper value to a function.|
|ZeroDivisionError||When the second operand of a division or modulo operator is zero.|
In this Python error article by TechVidan, we saw what Python error is and how it can affect the normal flow of execution of our program.
We also learned about Python exceptions and their types.
A good program should be able to handle any exception that may possibly occur.
We’ll learn how to handle these exceptions in another article.
Till then, keep coding and be careful of those errors!