Errors are the problems in a program due to which the program will stop the execution. On the other hand, exceptions are raised when the some internal events occur which changes the normal flow of the program.
Two types of Error occurs in python.
- Syntax errors
- Logical errors (Exceptions)
When the proper syntax of the language is not followed then syntax error is thrown.
It returns a syntax error message because after if statement a colon
: is missing. We can fix this by writing the correct syntax.
When in the runtime an error occurs after passing the syntax test is called exception or logical type. For example, when we divide any number by zero then
ZeroDivisionError exception is raised, or when we import a module that does not exist then
ImportError is raised.
In the above example the ZeroDivisionError as we are trying to divide a number by 0.
Example 2: When indentation is not correct.
Some of the common built-in exceptions are other than above mention exceptions are:
|IndexError||When the wrong index of a list is retrieved.|
|AssertionError||It occurs when assert statement fails|
|AttributeError||It occurs when an attribute assignment is failed.|
|ImportError||It occurs when an imported module is not found.|
|KeyError||It occurs when the key of the dictionary is not found.|
|NameError||It occurs when the variable is not defined.|
|MemoryError||It occurs when a program run out of memory.|
|TypeError||It occurs when a function and operation is applied in an incorrect type.|
Note: For more information, refer to Built-in Exceptions in Python
When an error and an exception is raised then we handle that with the help of Handling method.
- Handling Exceptions with Try/Except/Finally
We can handle error by Try/Except/Finally method.we write unsafe code in the try, fall back code in except and final code in finally block.
# put unsafe operation in try block
# unsafe operation perform
# if error occur the it goes in except block
"an error occurs"
# final code in finally block
code start an error occurs GeeksForGeeks
- Raising exceptions for a predefined condition
When we want to code for limitation of certain condition then we can raise an exception.
# try for unsafe code
# raise the ValueError
"please add money in your account"
"You are eligible to purchase DSA Self Paced course"
# if false then raise the value error
ValueError as e:
please add money in your account
- Built-in Exceptions in Python
- Exceptions - Selenium Python
- Python | Catching and Creating Exceptions
- User-defined Exceptions in Python with Examples
- Python - Read blob object in python using wand library
- Python | Index of Non-Zero elements in Python list
- Python | Merge Python key values to list
- Python | PRAW - Python Reddit API Wrapper
- Python | Convert list to Python array
- Reading Python File-Like Objects from C | Python
- Important differences between Python 2.x and Python 3.x with examples
- MySQL-Connector-Python module in Python
- twitter-text-python (ttp) module - Python
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Visualizing O(n) using Python
- Any All in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.