âšī¸ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.programiz.com/python-programming/exceptions |
| Last Crawled | 2026-04-16 02:09:33 (1 day ago) |
| First Indexed | 2016-12-10 18:38:57 (9 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Python Exceptions (With Examples) |
| Meta Description | In this tutorial, we will learn about exceptions in Python. We will cover exceptions and different types of exceptions in Python. |
| Meta Canonical | null |
| Boilerpipe Text | An exception is an unexpected event that occurs during program execution. For example,
divide_by_zero = 7 / 0
The above code causes an exception as it is not possible to divide a number by
0
.
Let's learn about Python Exceptions in detail.
Python Logical Errors (Exceptions)
Errors that occur at runtime (after passing the syntax test) are called
exceptions
or
logical errors
.
For instance, they occur when we
try to open a file(for reading) that does not exist (
FileNotFoundError
)
try to divide a number by zero (
ZeroDivisionError
)
try to import a module that does not exist (
ImportError
) and so on.
Whenever these types of runtime errors occur, Python creates an exception object.
If not handled properly, it prints a traceback to that error along with some details about why that error occurred.
Let's look at how Python treats these errors:
divide_numbers = 7 / 0
print(divide_numbers)
Output
Traceback (most recent call last):
File "<string>", line 1, in <module>
ZeroDivisionError: division by zero
Here, while trying to divide
7 / 0
, the program throws a system exception
ZeroDivisionError
Python Built-in Exceptions
Illegal operations can raise exceptions. There are plenty of built-in exceptions in Python that are raised when corresponding errors occur.
We can view all the built-in exceptions using the built-in
local()
function as follows:
print(dir(locals()['__builtins__']))
Here,
locals()['__builtins__']
will return a module of built-in exceptions,
functions
, and attributes and
dir
allows us to list these attributes as
strings
.
Some of the common built-in exceptions in Python programming along with the error that cause them are listed below:
Exception
Cause of Error
AssertionError
Raised when an
assert
statement fails.
AttributeError
Raised when attribute assignment or reference fails.
EOFError
Raised when the
input()
function hits end-of-file condition.
FloatingPointError
Raised when a floating point operation fails.
GeneratorExit
Raise when a generator's
close()
method is called.
ImportError
Raised when the imported module is not found.
IndexError
Raised when the index of a sequence is out of range.
KeyError
Raised when a key is not found in a dictionary.
KeyboardInterrupt
Raised when the user hits the interrupt key (
Ctrl+C
or
Delete
).
MemoryError
Raised when an operation runs out of memory.
NameError
Raised when a variable is not found in local or global scope.
NotImplementedError
Raised by abstract methods.
OSError
Raised when system operation causes system related error.
OverflowError
Raised when the result of an arithmetic operation is too large to be represented.
ReferenceError
Raised when a weak reference proxy is used to access a garbage collected referent.
RuntimeError
Raised when an error does not fall under any other category.
StopIteration
Raised by
next()
function to indicate that there is no further item to be returned by iterator.
SyntaxError
Raised by parser when syntax error is encountered.
IndentationError
Raised when there is incorrect indentation.
TabError
Raised when indentation consists of inconsistent tabs and spaces.
SystemError
Raised when interpreter detects internal error.
SystemExit
Raised by
sys.exit()
function.
TypeError
Raised when a function or operation is applied to an object of incorrect type.
UnboundLocalError
Raised when a reference is made to a local variable in a function or method, but no value has been bound to that variable.
UnicodeError
Raised when a Unicode-related encoding or decoding error occurs.
UnicodeEncodeError
Raised when a Unicode-related error occurs during encoding.
UnicodeDecodeError
Raised when a Unicode-related error occurs during decoding.
UnicodeTranslateError
Raised when a Unicode-related error occurs during translating.
ValueError
Raised when a function gets an argument of correct type but improper value.
ZeroDivisionError
Raised when the second operand of division or modulo operation is zero.
If required, we can also define our own exceptions in Python. To learn more about them, visit
Python User-defined Exceptions
.
We can handle these built-in and user-defined exceptions in Python using
try
,
except
and
finally
statements. To learn more about them, visit
Python try, except and finally statements
.
Python Error and Exception
Errors
represent conditions such as compilation error, syntax error, error in the logical part of the code, library incompatibility, infinite recursion, etc.
Errors are usually beyond the control of the programmer and we should not try to handle errors.
Exceptions
can be caught and handled by the program.
Now we know about exceptions, we will learn about handling exceptions in the next tutorial. |
| Markdown | 
[Stop copy pasting code you don't actually understand Build the coding confidence you need to become a developer companies will fight for Stop copy pasting code you don't actually understand Ends in Start FREE Trial Start FREE Trial Start FREE Trial Start FREE Trial](https://programiz.pro/?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=programiz&utm_content=default_banner&utm_term=sticky_banner_launch "Start FREE Trial")
[Stop copy pasting code you don't actually understand Build the coding confidence you need to become a developer companies will fight for Stop copy pasting code you don't actually understand Ends in Start FREE Trial Start FREE Trial Start FREE Trial Start FREE Trial](https://programiz.pro/?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=programiz&utm_content=default_banner&utm_term=sticky_banner_launch "Start FREE Trial")
[ ](https://www.programiz.com/ "Programiz")
[Tutorials](https://www.programiz.com/python-programming/exceptions "Programming Tutorials")
[Examples]("Programming Examples")
[ Courses](https://www.programiz.com/python-programming/exceptions "Learn to Code Interactively")
[Try Programiz PRO](https://programiz.pro/learn/master-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_floating_button "Try Programiz PRO")
Course Index
Tutorials
Courses
[Python]("Python") [JavaScript]("JavaScript") [TypeScript]("TypeScript") [SQL]("SQL") [HTML]("HTML") [CSS]("CSS") [C]("C") [C++]("C++") [Java]("Java") [R]("R") [Ruby]("Ruby") [RUST]("RUST") [Golang]("Golang") [Kotlin]("Kotlin") [Swift]("Swift") [C\#]("C#") [DSA]("DSA")
Become a certified Python
programmer.
[ENROLL](https://programiz.pro/learn/master-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner)
#### Popular Tutorials
[Getting Started With Python](https://www.programiz.com/python-programming/getting-started?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Getting Started With Python")
[Python if Statement](https://www.programiz.com/python-programming/if-elif-else?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Python if Statement")
[while Loop in Python](https://www.programiz.com/python-programming/while-loop?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "while Loop in Python")
[Python Lists](https://www.programiz.com/python-programming/list?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Python Lists")
[Dictionaries in Python](https://www.programiz.com/python-programming/dictionary?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Dictionaries in Python")
[Start Learning Python](https://www.programiz.com/python-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Python Tutorials")
#### Popular Examples
[Add two numbers](https://www.programiz.com/python-programming/examples/add-number?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Add two numbers")
[Check prime number](https://www.programiz.com/python-programming/examples/prime-number?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Check prime number")
[Find the factorial of a number](https://www.programiz.com/python-programming/examples/factorial?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Find the factorial of a number")
[Print the Fibonacci sequence](https://www.programiz.com/python-programming/examples/fibonacci-sequence?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Print the Fibonacci sequence")
[Check leap year](https://www.programiz.com/python-programming/examples/leap-year?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Check leap year")
[Explore Python Examples](https://www.programiz.com/python-programming/examples?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Python Examples")
#### Reference Materials
[Built-in Functions](https://www.programiz.com/python-programming/methods/built-in?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Built-in Functions")
[List Methods](https://www.programiz.com/python-programming/methods/list?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "List Methods")
[Dictionary Methods](https://www.programiz.com/python-programming/methods/dictionary?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Dictionary Methods")
[String Methods](https://www.programiz.com/python-programming/methods/string?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "String Methods")
[View all](https://www.programiz.com/python-programming/methods?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Python References")

Created with over a decade of experience.
- [Learn]("Learn")
- [Practice]("Practice")
- [Compete]("Compete")
[Learn Python](https://programiz.pro/learn/master-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_courses "Learn Python")
[Learn HTML](https://programiz.pro/course/learn-html?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_html&utm_term=nav_programiz-pro_courses "Learn HTML")
[Learn JavaScript](https://programiz.pro/learn/master-javascript?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_javascript&utm_term=nav_programiz-pro_courses "Learn JavaScript")
[Learn SQL](https://programiz.pro/course/learn-sql-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_sql&utm_term=nav_programiz-pro_courses "Learn SQL")
[Learn DSA](https://programiz.pro/course/dsa-with-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_dsa&utm_term=nav_programiz-pro_courses "Learn DSA")
[Learn C](https://programiz.pro/learn/master-c-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_c&utm_term=nav_programiz-pro_courses "Learn C")
[Learn C++](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_courses "Learn C++")
[Learn Java](https://programiz.pro/learn/master-java?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_courses "Learn Java")
[View all Courses on ](https://programiz.pro/courses?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "View all Courses on PRO")
[Python Basics](https://programiz.pro/course/practice-python-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_practice_courses "Python Basics")
[Python Intermediate](https://programiz.pro/course/practice-python-intermediate?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_practice_courses "Python Intermediate")
[C++ Basics](https://programiz.pro/course/practice-cpp-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_practice_courses "C++ Basics")
[C++ Intermediate](https://programiz.pro/course/practice-cpp-intermediate?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_practice_courses "C++ Intermediate")
[C++ OOP](https://programiz.pro/course/practice-cpp-oop?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_practice_courses "C++ OOP")
[C Programming](https://programiz.pro/course/practice-c-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_c&utm_term=nav_programiz-pro_practice_courses "C Programming")
[Java Basics](https://programiz.pro/course/practice-java-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_practice_courses "Java Basics")
[Java Intermediate](https://programiz.pro/course/practice-java-intermediate?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_practice_courses "Java Intermediate")
[Java OOP](https://programiz.pro/course/practice-java-oop?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=practice_course_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_practice_courses "Java OOP")
[View all Courses on ](https://programiz.pro/courses?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "View all Courses on PRO")
[Python Challenges](https://programiz.pro/community-challenges/python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_challenges "Python Challenges")
[JavaScript Challenges](https://programiz.pro/community-challenges/javascript?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_javascript&utm_term=nav_programiz-pro_challenges "JavaScript Challenges")
[Java Challenges](https://programiz.pro/community-challenges/java?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_challenges "Java Challenges")
[C++ Challenges](https://programiz.pro/community-challenges/cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_challenges "C++ Challenges")
[C Challenges](https://programiz.pro/community-challenges/c?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_c&utm_term=nav_programiz-pro_challenges "C Challenges")
[View all Challenges on ](https://programiz.pro/community-challenges?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "View all Challenges on PRO")
[Learn]()
[Practice]()
[Compete]()
#### Certification Courses
Created with over a decade of experience and thousands of feedback.
[Learn Python](https://programiz.pro/learn/master-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_python&utm_term=nav_programiz-pro_challenges "Learn Python")
[Learn HTML](https://programiz.pro/course/learn-html?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_html&utm_term=nav_programiz-pro_challenges "Learn HTML")
[Learn JavaScript](https://programiz.pro/learn/master-javascript?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_javascript&utm_term=nav_programiz-pro_challenges "Learn JavaScript")
[Learn SQL](https://programiz.pro/course/learn-sql-basics?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_sql&utm_term=nav_programiz-pro_challenges "Learn SQL")
[Learn DSA](https://programiz.pro/course/dsa-with-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_dsa&utm_term=nav_programiz-pro_challenges "Learn DSA")
[View all Courses on ](https://programiz.pro/courses?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "View all Courses on PRO")
[Learn C](https://programiz.pro/learn/master-c-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_c&utm_term=nav_programiz-pro_challenges "Learn C")
[Learn C++](https://programiz.pro/learn/master-cpp?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_cpp&utm_term=nav_programiz-pro_challenges "Learn C++")
[Learn Java](https://programiz.pro/learn/master-java?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=challenge_promotion&utm_content=interests_learn_java&utm_term=nav_programiz-pro_challenges "Learn Java")
[Python]("Python")
[JavaScript]("JavaScript")
[TypeScript]("TypeScript")
[SQL]("SQL")
[HTML]("HTML")
[CSS]("CSS")
[C]("C")
[C++]("C++")
[Java]("Java")
[More languages]("More languages")
### Become a certified Python programmer.
[Try Programiz PRO\!](https://programiz.pro/learn/master-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner)
#### Popular Tutorials
[Getting Started With Python](https://www.programiz.com/python-programming/getting-started?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Getting Started With Python")
[Python if Statement](https://www.programiz.com/python-programming/if-elif-else?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Python if Statement")
[while Loop in Python](https://www.programiz.com/python-programming/while-loop?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "while Loop in Python")
[Python Lists](https://www.programiz.com/python-programming/list?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Python Lists")
[Dictionaries in Python](https://www.programiz.com/python-programming/dictionary?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Dictionaries in Python")
[Start Learning Python](https://www.programiz.com/python-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Python tutorials")
[All Python Tutorials](https://www.programiz.com/python-programming?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "All Python tutorials")
#### Reference Materials
[Built-in Functions](https://www.programiz.com/python-programming/methods/built-in?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Built-in Functions")
[List Methods](https://www.programiz.com/python-programming/methods/list?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "List Methods")
[Dictionary Methods](https://www.programiz.com/python-programming/methods/dictionary?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Dictionary Methods")
[String Methods](https://www.programiz.com/python-programming/methods/string?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "String Methods")
[View all](https://www.programiz.com/python-programming/methods?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_tutorials_banner "Python References")
[Python]("Python")
[JavaScript]("JavaScript")
[C]("C")
[C++]("C++")
[Java]("Java")
[R]("R")
[Kotlin]("Kotlin")
Become a certified Python
programmer.
[Try Programiz PRO\!](https://programiz.pro/learn/master-python?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_examples_banner)
#### Popular Examples
[Add two numbers](https://www.programiz.com/python-programming/examples/add-number?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_examples_banner "Add two numbers")
[Check prime number](https://www.programiz.com/python-programming/examples/prime-number?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_examples_banner "Check prime number")
[Find the factorial of a number](https://www.programiz.com/python-programming/examples/factorial?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_examples_banner "Find the factorial of a number")
[Print the Fibonacci sequence](https://www.programiz.com/python-programming/examples/fibonacci-sequence?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_examples_banner "Print the Fibonacci sequence")
[Check leap year](https://www.programiz.com/python-programming/examples/leap-year?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_examples_banner "Check leap year")
[All Python Examples](https://www.programiz.com/python-programming/examples?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_campaign=course_promotion&utm_content=interests_learn_python&utm_term=nav_examples_banner "Python Examples")
- ### Python Introduction
- [Get Started With Python](https://www.programiz.com/python-programming/getting-started "Get Started With Python")
- [Your First Python Program](https://www.programiz.com/python-programming/first-program "Your First Python Program")
- [Python Comments](https://www.programiz.com/python-programming/comments "Python Comments")
- ### Python Fundamentals
- [Python Variables and Literals](https://www.programiz.com/python-programming/variables-constants-literals "Python Variables and Literals")
- [Python Type Conversion](https://www.programiz.com/python-programming/type-conversion-and-casting "Python Type Conversion")
- [Python Basic Input and Output](https://www.programiz.com/python-programming/input-output-import "Python Basic Input and Output")
- [Python Operators](https://www.programiz.com/python-programming/operators "Python Operators")
- ### Python Flow Control
- [Python if...else Statement](https://www.programiz.com/python-programming/if-elif-else "Python if...else Statement")
- [Python for Loop](https://www.programiz.com/python-programming/for-loop "Python for
Loop")
- [Python while Loop](https://www.programiz.com/python-programming/while-loop "Python
while Loop")
- [Python break and continue](https://www.programiz.com/python-programming/break-continue "Python break and continue")
- [Python pass Statement](https://www.programiz.com/python-programming/pass-statement "Python pass Statement")
- ### Python Data types
- [Python Numbers and Mathematics](https://www.programiz.com/python-programming/numbers "Python Numbers and Mathematics")
- [Python List](https://www.programiz.com/python-programming/list "Python List")
- [Python Tuple](https://www.programiz.com/python-programming/tuple "Python Tuple")
- [Python String](https://www.programiz.com/python-programming/string "Python String")
- [Python Set](https://www.programiz.com/python-programming/set "Python Set")
- [Python Dictionary](https://www.programiz.com/python-programming/dictionary "Python Dictionary")
- ### Python Functions
- [Python Functions](https://www.programiz.com/python-programming/function "Python Functions")
- [Python Function Arguments](https://www.programiz.com/python-programming/function-argument "Python Function Arguments")
- [Python Variable Scope](https://www.programiz.com/python-programming/global-local-nonlocal-variables "Python Variable Scope")
- [Python Global Keyword](https://www.programiz.com/python-programming/global-keyword "Python Global Keyword")
- [Python Recursion](https://www.programiz.com/python-programming/recursion "Python Recursion")
- [Python Modules](https://www.programiz.com/python-programming/modules "Python Modules")
- [Python Package](https://www.programiz.com/python-programming/package "Python Package")
- [Python Main function](https://www.programiz.com/python-programming/main-function "Python Main function")
- ### Python Files
- [Python Directory and Files Management](https://www.programiz.com/python-programming/directory "Python Directory and Files Management")
- [Python CSV: Read and Write CSV files](https://www.programiz.com/python-programming/csv "Python CSV: Read and Write CSV files")
- [Reading CSV files in Python](https://www.programiz.com/python-programming/reading-csv-files "Reading CSV files in Python")
- [Writing CSV files in Python](https://www.programiz.com/python-programming/writing-csv-files "Writing CSV files in Python")
- ### Python Exception Handling
- [Python Exceptions](https://www.programiz.com/python-programming/exceptions "Python Exceptions")
- [Python Exception Handling](https://www.programiz.com/python-programming/exception-handling "Python Exception Handling")
- [Python Custom Exceptions](https://www.programiz.com/python-programming/user-defined-exception "Python Custom Exceptions")
- ### Python Object & Class
- [Python Objects and Classes](https://www.programiz.com/python-programming/class "Python Objects and Classes")
- [Python Inheritance](https://www.programiz.com/python-programming/inheritance "Python
Inheritance")
- [Python Multiple Inheritance](https://www.programiz.com/python-programming/multiple-inheritance "Python Multiple Inheritance")
- [Polymorphism in Python](https://www.programiz.com/python-programming/polymorphism "Polymorphism in Python")
- [Python Operator Overloading](https://www.programiz.com/python-programming/operator-overloading "Python Operator Overloading")
- ### Python Advanced Topics
- [List comprehension](https://www.programiz.com/python-programming/list-comprehension "List comprehension")
- [Python Lambda/Anonymous Function](https://www.programiz.com/python-programming/anonymous-function "Python Lambda/Anonymous Function")
- [Python Iterators](https://www.programiz.com/python-programming/iterator "Python Iterators")
- [Python Generators](https://www.programiz.com/python-programming/generator "Python Generators")
- [Python Namespace and Scope](https://www.programiz.com/python-programming/namespace "Python Namespace and Scope")
- [Python Closures](https://www.programiz.com/python-programming/closure "Python Closures")
- [Python Decorators](https://www.programiz.com/python-programming/decorator "Python Decorators")
- [Python @property decorator](https://www.programiz.com/python-programming/property "Python @property decorator")
- [Python RegEx](https://www.programiz.com/python-programming/regex "Python RegEx")
- ### Python Date and Time
- [Python datetime](https://www.programiz.com/python-programming/datetime "Python
datetime")
- [Python strftime()](https://www.programiz.com/python-programming/datetime/strftime "Python
strftime()")
- [Python strptime()](https://www.programiz.com/python-programming/datetime/strptime "Python strptime()")
- [How to get current date and time in Python?](https://www.programiz.com/python-programming/datetime/current-datetime "How to get current date and time in Python?")
- [Python Get Current Time](https://www.programiz.com/python-programming/datetime/current-time "Python Get Current Time")
- [Python timestamp to datetime and vice-versa](https://www.programiz.com/python-programming/datetime/timestamp-datetime "Python timestamp to datetime and vice-versa")
- [Python time Module](https://www.programiz.com/python-programming/time "Python time
Module")
- [Python sleep()](https://www.programiz.com/python-programming/time/sleep "Python
sleep()")
- ### Additional Topic
- [Precedence and Associativity of Operators in Python](https://www.programiz.com/python-programming/precedence-associativity "Precedence and Associativity of Operators in Python ")
- [Python Keywords and Identifiers](https://www.programiz.com/python-programming/keywords-identifier "Python Keywords and Identifiers")
- [Python Asserts](https://www.programiz.com/python-programming/assert-statement "Python Asserts")
- [Python Json](https://www.programiz.com/python-programming/json "Python Json")
- [Python pip](https://www.programiz.com/python-programming/pip "Python pip")
- [Python \*args and \*\*kwargs](https://www.programiz.com/python-programming/args-and-kwargs "Python *args and **kwargs ")
### Python Tutorials
- [Python Custom Exceptions](https://www.programiz.com/python-programming/user-defined-exception)
- [Python Exception Handling](https://www.programiz.com/python-programming/exception-handling)
- [Python open()](https://www.programiz.com/python-programming/methods/built-in/open)
- [List of Keywords in Python](https://www.programiz.com/python-programming/keyword-list)
- [Python pow()](https://www.programiz.com/python-programming/methods/built-in/pow)
- [Python String encode()](https://www.programiz.com/python-programming/methods/string/encode)
[](https://programiz.pro/course/learn-python-visually?utm_source=programiz.com&utm_medium=referral&utm_campaign=python_with_visualizer_launch_2025&utm_content=interests_learn_python&utm_term=top-most-ad-spot-python-2)
# Python Exceptions
An exception is an unexpected event that occurs during program execution. For example,
```
divide_by_zero = 7 / 0
```
The above code causes an exception as it is not possible to divide a number by **0**.
Let's learn about Python Exceptions in detail.
***
## Python Logical Errors (Exceptions)
Errors that occur at runtime (after passing the syntax test) are called **exceptions** or **logical errors**.
For instance, they occur when we
- try to open a file(for reading) that does not exist (`FileNotFoundError`)
- try to divide a number by zero (`ZeroDivisionError`)
- try to import a module that does not exist (`ImportError`) and so on.
Whenever these types of runtime errors occur, Python creates an exception object.
If not handled properly, it prints a traceback to that error along with some details about why that error occurred.
Let's look at how Python treats these errors:
```
divide_numbers = 7 / 0
print(divide_numbers)
```
**Output**
```
Traceback (most recent call last): File "<string>", line 1, in <module> ZeroDivisionError: division by zero
```
Here, while trying to divide `7 / 0`, the program throws a system exception `ZeroDivisionError`
***
## Python Built-in Exceptions
Illegal operations can raise exceptions. There are plenty of built-in exceptions in Python that are raised when corresponding errors occur.
We can view all the built-in exceptions using the built-in `local()` function as follows:
```
print(dir(locals()['__builtins__']))
```
Here, `locals()['__builtins__']` will return a module of built-in exceptions, [functions](https://www.programiz.com/python-programming/function), and attributes and `dir` allows us to list these attributes as [strings](https://www.programiz.com/python-programming/string).
Some of the common built-in exceptions in Python programming along with the error that cause them are listed below:
| Exception | Cause of Error |
|---|---|
| `AssertionError` | Raised when an `assert` statement fails. |
| `AttributeError` | Raised when attribute assignment or reference fails. |
| `EOFError` | Raised when the `input()` function hits end-of-file condition. |
| `FloatingPointError` | Raised when a floating point operation fails. |
| `GeneratorExit` | Raise when a generator's `close()` method is called. |
| `ImportError` | Raised when the imported module is not found. |
| `IndexError` | Raised when the index of a sequence is out of range. |
| `KeyError` | Raised when a key is not found in a dictionary. |
| `KeyboardInterrupt` | Raised when the user hits the interrupt key (`Ctrl+C` or `Delete`). |
| `MemoryError` | Raised when an operation runs out of memory. |
| `NameError` | Raised when a variable is not found in local or global scope. |
| `NotImplementedError` | Raised by abstract methods. |
| `OSError` | Raised when system operation causes system related error. |
| `OverflowError` | Raised when the result of an arithmetic operation is too large to be represented. |
| `ReferenceError` | Raised when a weak reference proxy is used to access a garbage collected referent. |
| `RuntimeError` | Raised when an error does not fall under any other category. |
| `StopIteration` | Raised by `next()` function to indicate that there is no further item to be returned by iterator. |
| `SyntaxError` | Raised by parser when syntax error is encountered. |
| `IndentationError` | Raised when there is incorrect indentation. |
| `TabError` | Raised when indentation consists of inconsistent tabs and spaces. |
| `SystemError` | Raised when interpreter detects internal error. |
| `SystemExit` | Raised by `sys.exit()` function. |
| `TypeError` | Raised when a function or operation is applied to an object of incorrect type. |
| `UnboundLocalError` | Raised when a reference is made to a local variable in a function or method, but no value has been bound to that variable. |
| `UnicodeError` | Raised when a Unicode-related encoding or decoding error occurs. |
| `UnicodeEncodeError` | Raised when a Unicode-related error occurs during encoding. |
| `UnicodeDecodeError` | Raised when a Unicode-related error occurs during decoding. |
| `UnicodeTranslateError` | Raised when a Unicode-related error occurs during translating. |
| `ValueError` | Raised when a function gets an argument of correct type but improper value. |
| `ZeroDivisionError` | Raised when the second operand of division or modulo operation is zero. |
If required, we can also define our own exceptions in Python. To learn more about them, visit [Python User-defined Exceptions](https://www.programiz.com/python-programming/user-defined-exception).
We can handle these built-in and user-defined exceptions in Python using `try`, `except` and `finally` statements. To learn more about them, visit [Python try, except and finally statements](https://www.programiz.com/python-programming/exception-handling).
***
## Python Error and Exception
**Errors** represent conditions such as compilation error, syntax error, error in the logical part of the code, library incompatibility, infinite recursion, etc.
Errors are usually beyond the control of the programmer and we should not try to handle errors.
**Exceptions** can be caught and handled by the program.
Now we know about exceptions, we will learn about handling exceptions in the next tutorial.
### Table of Contents
- [Introduction](https://www.programiz.com/python-programming/exceptions#introduction)
- [Python Logical Errors (Exceptions)](https://www.programiz.com/python-programming/exceptions#logical)
- [Python Built-in Exceptions](https://www.programiz.com/python-programming/exceptions#built-in)
- [Python Error and Exception](https://www.programiz.com/python-programming/exceptions#error-exception)
## Video: Python Exception Handling
[Previous Tutorial: Writing CSV files in Python](https://www.programiz.com/python-programming/writing-csv-files "Writing CSV files in Python")
[Next Tutorial: Python Exception Handling](https://www.programiz.com/python-programming/exception-handling "Python Exception Handling")
Share on:
Did you find this article helpful?
Your builder path starts here. Builders don't just know how to code, they create solutions that matter.
Escape tutorial hell and ship real projects.
[Try Programiz PRO](https://programiz.pro/?utm_source=programiz.com&utm_medium=referral&utm_audience=ORGANIC-FREEMIUM&utm_content=interests_learn_coding&utm_term=tutorial_page_footer_banner "Programiz PRO: Premium Learning Platform from Programiz")
- Real-World Projects
- On-Demand Learning
- AI Mentor
- Builder Community
### Related Tutorials
[Python Tutorial Python Custom Exceptions](https://www.programiz.com/python-programming/user-defined-exception)
[Python Tutorial Python Exception Handling](https://www.programiz.com/python-programming/exception-handling)
[Python Library Python open()](https://www.programiz.com/python-programming/methods/built-in/open)
[Python Tutorial List of Keywords in Python](https://www.programiz.com/python-programming/keyword-list)
#### Free Tutorials
- [Python 3 Tutorials](https://www.programiz.com/python-programming "Python 3 Tutorials")
- [SQL Tutorials](https://www.programiz.com/sql "SQL Tutorials")
- [R Tutorials](https://www.programiz.com/r "R Tutorials")
- [HTML Tutorials](https://www.programiz.com/html "HTML Tutorials")
- [CSS Tutorials](https://www.programiz.com/css "CSS Tutorials")
- [JavaScript Tutorials](https://www.programiz.com/javascript "JavaScript Tutorials")
- [TypeScript Tutorials](https://www.programiz.com/typescript "TypeScript Tutorials")
- [Java Tutorials](https://www.programiz.com/java-programming "Java Tutorials")
- [C Tutorials](https://www.programiz.com/c-programming "C Tutorials")
- [C++ Tutorials](https://www.programiz.com/cpp-programming "C++ Tutorials")
- [DSA Tutorials](https://www.programiz.com/dsa "Data Structures and Algorithms")
- [C\# Tutorials](https://www.programiz.com/csharp-programming "C# Tutorials")
- [Golang Tutorials](https://www.programiz.com/golang "Golang Tutorials")
- [Kotlin Tutorials](https://www.programiz.com/kotlin-programming "Kotlin Tutorials")
- [Swift Tutorials](https://www.programiz.com/swift-programming "Swift Tutorials")
- [Rust Tutorials](https://www.programiz.com/rust "Rust Tutorials")
- [Ruby Tutorials](https://www.programiz.com/ruby "Ruby Tutorials")
#### Paid Courses
- [Master Python](https://programiz.pro/learn/master-python "Master Python")
- [Learn SQL](https://programiz.pro/course/learn-sql-basics "Learn SQL")
- [Learn HTML](https://programiz.pro/course/learn-html "Learn HTML")
- [Master JavaScript](https://programiz.pro/learn/master-javascript "Master JavaScript")
- [Master C](https://programiz.pro/learn/master-c-programming "Master C")
- [Master C++](https://programiz.pro/learn/master-cpp "Master C++")
- [Master Java](https://programiz.pro/learn/master-java "Master Java")
- [Master DSA with Python](https://programiz.pro/learn/master-dsa-with-python "Master DSA with Python")
#### Online Compilers
- [Python Compiler](https://www.programiz.com/python-programming/online-compiler "Python Compiler")
- [R Compiler](https://www.programiz.com/r/online-compiler "R Compiler")
- [SQL Editor](https://www.programiz.com/sql/online-compiler "SQL Editor")
- [HTML/CSS Editor](https://www.programiz.com/html/online-compiler "HTML/CSS Editor")
- [JavaScript Editor](https://www.programiz.com/javascript/online-compiler "JavaScript Editor")
- [TypeScript Editor](https://www.programiz.com/typescript/online-compiler "TypeScript Editor")
- [Java Compiler](https://www.programiz.com/java-programming/online-compiler "Java Compiler")
- [C Compiler](https://www.programiz.com/c-programming/online-compiler "C Compiler")
- [C++ Compiler](https://www.programiz.com/cpp-programming/online-compiler "C++ Compiler")
- [C\# Compiler](https://www.programiz.com/csharp-programming/online-compiler "C# Compiler")
- [Go Compiler](https://www.programiz.com/golang/online-compiler "Go Compiler")
- [PHP Compiler](https://www.programiz.com/php/online-compiler "PHP Compiler")
- [Swift Compiler](https://www.programiz.com/swift/online-compiler "Swift Compiler")
- [Rust Compiler](https://www.programiz.com/rust/online-compiler "Rust Compiler")
- [Ruby Compiler](https://www.programiz.com/ruby/online-compiler "Ruby Compiler")
#### Mobile Apps
- [Learn Python App](https://www.programiz.com/learn-python "Learn Python: Programiz")
- [Learn C App](https://www.programiz.com/learn-c "Learn C Programming: Programiz")
- [Learn Java App](https://www.programiz.com/learn-java "Learn Java: Programiz")
- [Learn C++ App](https://www.programiz.com/learn-cpp "Learn C++: Programiz")
#### Company
- [Change Ad Consent]()
- [Do not sell my data]()
- [About](https://www.programiz.com/about-us "About us")
- [Contact](https://www.programiz.com/contact "Contact us")
- [Blog](https://www.programiz.com/blog "Blog")
- [Youtube](https://www.youtube.com/channel/UCREFp3D_n8JfcDonlm7Mpyw "Programiz on Youtube")
- [Careers](https://www.programiz.com/careers "Careers")
- [Advertising](https://www.programiz.com/advertise "Advertise with us")
- [Privacy Policy](https://www.programiz.com/privacy-policy "Privacy Policy")
- [Terms & Conditions](https://www.programiz.com/terms-of-use "Terms & Conditions")
Š Parewa Labs Pvt. Ltd. All rights reserved. |
| Readable Markdown | An exception is an unexpected event that occurs during program execution. For example,
```
divide_by_zero = 7 / 0
```
The above code causes an exception as it is not possible to divide a number by **0**.
Let's learn about Python Exceptions in detail.
***
## Python Logical Errors (Exceptions)
Errors that occur at runtime (after passing the syntax test) are called **exceptions** or **logical errors**.
For instance, they occur when we
- try to open a file(for reading) that does not exist (`FileNotFoundError`)
- try to divide a number by zero (`ZeroDivisionError`)
- try to import a module that does not exist (`ImportError`) and so on.
Whenever these types of runtime errors occur, Python creates an exception object.
If not handled properly, it prints a traceback to that error along with some details about why that error occurred.
Let's look at how Python treats these errors:
```
divide_numbers = 7 / 0
print(divide_numbers)
```
**Output**
```
Traceback (most recent call last): File "<string>", line 1, in <module> ZeroDivisionError: division by zero
```
Here, while trying to divide `7 / 0`, the program throws a system exception `ZeroDivisionError`
***
## Python Built-in Exceptions
Illegal operations can raise exceptions. There are plenty of built-in exceptions in Python that are raised when corresponding errors occur.
We can view all the built-in exceptions using the built-in `local()` function as follows:
```
print(dir(locals()['__builtins__']))
```
Here, `locals()['__builtins__']` will return a module of built-in exceptions, [functions](https://www.programiz.com/python-programming/function), and attributes and `dir` allows us to list these attributes as [strings](https://www.programiz.com/python-programming/string).
Some of the common built-in exceptions in Python programming along with the error that cause them are listed below:
| Exception | Cause of Error |
|---|---|
| `AssertionError` | Raised when an `assert` statement fails. |
| `AttributeError` | Raised when attribute assignment or reference fails. |
| `EOFError` | Raised when the `input()` function hits end-of-file condition. |
| `FloatingPointError` | Raised when a floating point operation fails. |
| `GeneratorExit` | Raise when a generator's `close()` method is called. |
| `ImportError` | Raised when the imported module is not found. |
| `IndexError` | Raised when the index of a sequence is out of range. |
| `KeyError` | Raised when a key is not found in a dictionary. |
| `KeyboardInterrupt` | Raised when the user hits the interrupt key (`Ctrl+C` or `Delete`). |
| `MemoryError` | Raised when an operation runs out of memory. |
| `NameError` | Raised when a variable is not found in local or global scope. |
| `NotImplementedError` | Raised by abstract methods. |
| `OSError` | Raised when system operation causes system related error. |
| `OverflowError` | Raised when the result of an arithmetic operation is too large to be represented. |
| `ReferenceError` | Raised when a weak reference proxy is used to access a garbage collected referent. |
| `RuntimeError` | Raised when an error does not fall under any other category. |
| `StopIteration` | Raised by `next()` function to indicate that there is no further item to be returned by iterator. |
| `SyntaxError` | Raised by parser when syntax error is encountered. |
| `IndentationError` | Raised when there is incorrect indentation. |
| `TabError` | Raised when indentation consists of inconsistent tabs and spaces. |
| `SystemError` | Raised when interpreter detects internal error. |
| `SystemExit` | Raised by `sys.exit()` function. |
| `TypeError` | Raised when a function or operation is applied to an object of incorrect type. |
| `UnboundLocalError` | Raised when a reference is made to a local variable in a function or method, but no value has been bound to that variable. |
| `UnicodeError` | Raised when a Unicode-related encoding or decoding error occurs. |
| `UnicodeEncodeError` | Raised when a Unicode-related error occurs during encoding. |
| `UnicodeDecodeError` | Raised when a Unicode-related error occurs during decoding. |
| `UnicodeTranslateError` | Raised when a Unicode-related error occurs during translating. |
| `ValueError` | Raised when a function gets an argument of correct type but improper value. |
| `ZeroDivisionError` | Raised when the second operand of division or modulo operation is zero. |
If required, we can also define our own exceptions in Python. To learn more about them, visit [Python User-defined Exceptions](https://www.programiz.com/python-programming/user-defined-exception).
We can handle these built-in and user-defined exceptions in Python using `try`, `except` and `finally` statements. To learn more about them, visit [Python try, except and finally statements](https://www.programiz.com/python-programming/exception-handling).
***
## Python Error and Exception
**Errors** represent conditions such as compilation error, syntax error, error in the logical part of the code, library incompatibility, infinite recursion, etc.
Errors are usually beyond the control of the programmer and we should not try to handle errors.
**Exceptions** can be caught and handled by the program.
Now we know about exceptions, we will learn about handling exceptions in the next tutorial. |
| Shard | 35 (laksa) |
| Root Hash | 16786490165506891835 |
| Unparsed URL | com,programiz!www,/python-programming/exceptions s443 |