Python Programming : Errors and Exceptions
Syntax Errors in Python
In your code, when you mess up the rules of Python Syntax, your code doesn’t run. The following code causes a syntax error.
>>> if 2>1 print("2")
SyntaxError: invalid syntax
This code doesn’t run because it misses a colon after the condition 2>1.
A syntax error also called a parsing error, displays ‘Syntax Error: invalid syntax’.
Let us revise introduction to Python
It covers all possible python errors and python exceptions to help you in running your python code smoothly as there are many reasons why to learn Python.
Introduction to Python Exceptions
It may be convenient to recognize the problems in your python code before you put it to real use. But that does not always happen. Sometimes, problems show up when you run the code; sometimes, midway of that.
An exception is an error that’s detected during execution. It may be fatal for the program, but not necessarily so. Let’s take the most common example.
Steps to Install Python on Windows
- >>> a,b=1,0
- >>> print(a/b)
- Traceback (most recent call last):
- File "<pyshell#208>", line 1, in <module>
- print(a/b)
ZeroDivisionError: division by zero
Throughout our python tutorials so far, you’ve noticed words like TypeError, NameError, and so. It’s time to find out what that is.
Message for Errors and Exceptions in Python
When errors and exceptions in python occurs, it prints a four-line message on the screen, if not handled.
The first line declares that this is a traceback. This means that the interpreter traces the exception back to its source.
The second tells us the line number for the code that caused the exception. In our case, it is line 1. #208 means this is the 208th statement we’re running in the interpreter since we opened it.
The third line tells us which line (the statement that) caused the exception.
Finally, the fourth line tells us the type of exception that occurred. This is accompanied by a short description to what happened.
In-built Exceptions in Python
Now that we know what an exception is, we will talk of a list of python exceptions that are in-built in Python. As you read the list, try to recall if you ever encountered any of these exceptions. Tell us in the comments.
Learn more about exception handling in Python
a. AssertionError in python
This exception raises when an assert statement fails. A successful assert statement looks like this:
>>> assert(1==1)
But when we write the following code, we get an AssertionError:
- >>> assert(1==2)
- Traceback (most recent call last):
- File "<pyshell#213>", line 1, in <module>
- assert(1==2)
AssertionError
We’ll take the assert statement in detail in a future lesson.
b. AttributeError in python
This one occurs when an attribute assignment or reference fails. As an example, let’s take class ‘fruit’.
- >>> class fruit:
- pass
- >>> fruit.size
- Traceback (most recent call last):
- File "<pyshell#223>", line 1, in <module>
- fruit.size
AttributeError: type object ‘fruit’ has no attribute ‘size’
Here, the attribute size does not exist. Hence, it raises an AttributeError.
Let us see the tremendous Python career opportunities in 2018
c. EOFError in Python
This exception raises when the input() function reaches the end-of-file condition.
d. FloatingPointError in Python
When a floating point operation fails, this exception occurs.
e. GeneratorExit in python
This raises when a generator’s close() method is called.
f. ImportError in Python
When the imported module isn’t found, an ImportError occurs.
- >>> from math import ppi
- Traceback (most recent call last):
- File "<pyshell#234>", line 1, in <module>
- from math import ppi
ImportError: cannot import name ‘ppi’
g. IndexErrorin Python
When you access an index, on a sequence, that is out of range, you get an IndexError.
- >>> list=[1,2,3]
- >>> list[3]
- Traceback (most recent call last):
- File "<pyshell#236>", line 1, in <module>
- list[3]
IndexError: list index out of range
Refer top 10 Python books to learn Python programming
Related Posts-
Support Engineer at Salesforce
7yTq for sharing