ℹ️ 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/exception-handling |
| Last Crawled | 2026-04-16 02:01:49 (1 day ago) |
| First Indexed | 2016-11-23 23:12:35 (9 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Python Exception Handling (With Examples) |
| Meta Description | In the tutorial, we will learn about different approaches of exception handling in Python with the help of examples. |
| Meta Canonical | null |
| Boilerpipe Text | In the last tutorial, we learned about
Python exceptions
. We know that exceptions abnormally terminate the execution of a program.
Since exceptions abnormally terminate the execution of a program, it is important to handle exceptions. In Python, we use the
try...except
block to handle exceptions.
Python try...except Block
The
try...except
block is used to handle exceptions in Python. Here's the syntax of
try...except
block:
try:
# code that may cause exception
except:
# code to run when exception occurs
Here, we have placed the code that might generate an exception inside the
try
block. Every
try
block is followed by an
except
block.
When an exception occurs, it is caught by the
except
block. The
except
block cannot be used without the try block.
Example: Exception Handling Using try...except
try:
numerator = 10
denominator = 0
result = numerator/denominator
print(result)
except:
print("Error: Denominator cannot be 0.")
# Output: Error: Denominator cannot be 0.
In the example, we are trying to divide a number by
0
. Here, this code generates an exception.
To handle the exception, we have put the code,
result = numerator/denominator
inside the
try
block. Now when an exception occurs, the rest of the code inside the
try
block is skipped.
The
except
block catches the exception and statements inside the
except
block are executed.
If none of the statements in the
try
block generates an exception, the
except
block is skipped.
Catching Specific Exceptions in Python
For each
try
block, there can be zero or more
except
blocks. Multiple
except
blocks allow us to handle each exception differently.
The argument type of each
except
block indicates the type of exception that can be handled by it. For example,
try:
even_numbers = [2,4,6,8]
print(even_numbers[5])
except ZeroDivisionError:
print("Denominator cannot be 0.")
except IndexError:
print("Index Out of Bound.")
# Output: Index Out of Bound
In this example, we have created a
list
named
even_numbers
.
Since the list index starts from
0
, the last element of the list is at index
3
. Notice the statement,
print(even_numbers[5])
Here, we are trying to access a value to the index
5
. Hence,
IndexError
exception occurs.
When the
IndexError
exception occurs in the
try
block,
The
ZeroDivisionError
exception is skipped.
The set of code inside the
IndexError
exception is executed.
Python try with else clause
In some situations, we might want to run a certain block of code if the code block inside
try
runs without any errors.
For these cases, you can use the optional
else
keyword with the
try
statement.
Let's look at an example:
# program to print the reciprocal of even numbers
try:
num = int(input("Enter a number: "))
assert num % 2 == 0
except:
print("Not an even number!")
else:
reciprocal = 1/num
print(reciprocal)
Output
If we pass an odd number:
Enter a number: 1
Not an even number!
If we pass an even number, the reciprocal is computed and displayed.
Enter a number: 4
0.25
However, if we pass
0
, we get
ZeroDivisionError
as the code block inside
else
is not handled by preceding
except
.
Enter a number: 0
Traceback (most recent call last):
File "<string>", line 7, in <module>
reciprocal = 1/num
ZeroDivisionError: division by zero
Here, the
assert
statement in the code checks that
num
is an even number; if num is odd, it raises an
AssertionError
, triggering the except block.
Note
: Exceptions in the
else
clause are not handled by the preceding except clauses.
Python try...finally
In Python, the
finally
block is always executed no matter whether there is an exception or not.
The
finally
block is optional. And, for each
try
block, there can be only one
finally
block.
Let's see an example,
try:
numerator = 10
denominator = 0
result = numerator/denominator
print(result)
except:
print("Error: Denominator cannot be 0.")
finally:
print("This is finally block.")
Output
Error: Denominator cannot be 0.
This is finally block.
In the above example, we are dividing a number by
0
inside the
try
block. Here, this code generates an exception.
The exception is caught by the
except
block. And, then the
finally
block is executed.
Also Read:
Python built-in Exception
Python user-defined Exception |
| 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/exception-handling "Programming Tutorials")
[Examples]("Programming Examples")
[ Courses](https://www.programiz.com/python-programming/exception-handling "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 Exceptions](https://www.programiz.com/python-programming/exceptions)
- [List of Keywords in Python](https://www.programiz.com/python-programming/keyword-list)
- [Python match…case Statement](https://www.programiz.com/python-programming/match-case)
- [Python if...else Statement](https://www.programiz.com/python-programming/if-elif-else)
- [Python String index()](https://www.programiz.com/python-programming/methods/string/index)
[](https://programiz.pro/dsa-with-visualizer?utm_source=programiz.com&utm_medium=referral&utm_campaign=dsa_with_visualizer_launch_2025&utm_content=interests_learn_dsa&utm_term=top-most-ad-spot-dsa-3)
# Python Exception Handling
In the last tutorial, we learned about [Python exceptions](https://www.programiz.com/python-programming/exceptions). We know that exceptions abnormally terminate the execution of a program.
Since exceptions abnormally terminate the execution of a program, it is important to handle exceptions. In Python, we use the `try...except` block to handle exceptions.
***
## Python try...except Block
The `try...except` block is used to handle exceptions in Python. Here's the syntax of `try...except` block:
```
try:
# code that may cause exception
except:
# code to run when exception occurs
```
Here, we have placed the code that might generate an exception inside the `try` block. Every `try` block is followed by an `except` block.
When an exception occurs, it is caught by the `except` block. The `except` block cannot be used without the try block.
***
## Example: Exception Handling Using try...except
```
try:
numerator = 10
denominator = 0
result = numerator/denominator
print(result)
except:
print("Error: Denominator cannot be 0.")
# Output: Error: Denominator cannot be 0.
```
In the example, we are trying to divide a number by **0**. Here, this code generates an exception.
To handle the exception, we have put the code, `result = numerator/denominator` inside the `try` block. Now when an exception occurs, the rest of the code inside the `try` block is skipped.
The `except` block catches the exception and statements inside the `except` block are executed.
If none of the statements in the `try` block generates an exception, the `except` block is skipped.
***
## Catching Specific Exceptions in Python
For each `try` block, there can be zero or more `except` blocks. Multiple `except` blocks allow us to handle each exception differently.
The argument type of each `except` block indicates the type of exception that can be handled by it. For example,
```
try:
even_numbers = [2,4,6,8]
print(even_numbers[5])
except ZeroDivisionError:
print("Denominator cannot be 0.")
except IndexError:
print("Index Out of Bound.")
# Output: Index Out of Bound
```
In this example, we have created a [list](https://www.programiz.com/python-programming/list) named even\_numbers.
Since the list index starts from **0**, the last element of the list is at index **3**. Notice the statement,
```
print(even_numbers[5])
```
Here, we are trying to access a value to the index **5**. Hence, `IndexError` exception occurs.
When the `IndexError` exception occurs in the `try` block,
- The `ZeroDivisionError` exception is skipped.
- The set of code inside the `IndexError` exception is executed.
***
## Python try with else clause
In some situations, we might want to run a certain block of code if the code block inside `try` runs without any errors.
For these cases, you can use the optional `else` keyword with the `try` statement.
Let's look at an example:
```
# program to print the reciprocal of even numbers
try:
num = int(input("Enter a number: "))
assert num % 2 == 0
except:
print("Not an even number!")
else:
reciprocal = 1/num
print(reciprocal)
```
**Output**
If we pass an odd number:
```
Enter a number: 1 Not an even number!
```
If we pass an even number, the reciprocal is computed and displayed.
```
Enter a number: 4 0.25
```
However, if we pass **0**, we get `ZeroDivisionError` as the code block inside `else` is not handled by preceding `except`.
```
Enter a number: 0 Traceback (most recent call last): File "<string>", line 7, in <module> reciprocal = 1/num ZeroDivisionError: division by zero
```
Here, the [assert](https://www.programiz.com/python-programming/assert-statement) statement in the code checks that `num` is an even number; if num is odd, it raises an `AssertionError`, triggering the except block.
**Note**: Exceptions in the `else` clause are not handled by the preceding except clauses.
***
## Python try...finally
In Python, the `finally` block is always executed no matter whether there is an exception or not.
The `finally` block is optional. And, for each `try` block, there can be only one `finally` block.
Let's see an example,
```
try:
numerator = 10
denominator = 0
result = numerator/denominator
print(result)
except:
print("Error: Denominator cannot be 0.")
finally:
print("This is finally block.")
```
**Output**
```
Error: Denominator cannot be 0. This is finally block.
```
In the above example, we are dividing a number by **0** inside the `try` block. Here, this code generates an exception.
The exception is caught by the `except` block. And, then the `finally` block is executed.
***
**Also Read:**
- [Python built-in Exception](https://www.programiz.com/python-programming/exceptions)
- [Python user-defined Exception](https://www.programiz.com/python-programming/user-defined-exception)
### Table of Contents
- [Introduction](https://www.programiz.com/python-programming/exception-handling#introduction)
- [Python try...except Block](https://www.programiz.com/python-programming/exception-handling#try-except)
- [Catching Specific Exceptions in Python](https://www.programiz.com/python-programming/exception-handling#specific)
- [Python try with else clause](https://www.programiz.com/python-programming/exception-handling#else)
- [Python try...finally](https://www.programiz.com/python-programming/exception-handling#try)
## Video: Python Exception Handling (try..except..finally)
[Previous Tutorial: Python Exceptions](https://www.programiz.com/python-programming/exceptions "Python Exceptions")
[Next Tutorial: Python Custom Exceptions](https://www.programiz.com/python-programming/user-defined-exception "Python Custom Exceptions")
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 Exceptions](https://www.programiz.com/python-programming/exceptions)
[Python Tutorial List of Keywords in Python](https://www.programiz.com/python-programming/keyword-list)
[Python Tutorial Python match…case Statement](https://www.programiz.com/python-programming/match-case)
#### 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 | In the last tutorial, we learned about [Python exceptions](https://www.programiz.com/python-programming/exceptions). We know that exceptions abnormally terminate the execution of a program.
Since exceptions abnormally terminate the execution of a program, it is important to handle exceptions. In Python, we use the `try...except` block to handle exceptions.
***
## Python try...except Block
The `try...except` block is used to handle exceptions in Python. Here's the syntax of `try...except` block:
```
try:
# code that may cause exception
except:
# code to run when exception occurs
```
Here, we have placed the code that might generate an exception inside the `try` block. Every `try` block is followed by an `except` block.
When an exception occurs, it is caught by the `except` block. The `except` block cannot be used without the try block.
***
## Example: Exception Handling Using try...except
```
try:
numerator = 10
denominator = 0
result = numerator/denominator
print(result)
except:
print("Error: Denominator cannot be 0.")
# Output: Error: Denominator cannot be 0.
```
In the example, we are trying to divide a number by **0**. Here, this code generates an exception.
To handle the exception, we have put the code, `result = numerator/denominator` inside the `try` block. Now when an exception occurs, the rest of the code inside the `try` block is skipped.
The `except` block catches the exception and statements inside the `except` block are executed.
If none of the statements in the `try` block generates an exception, the `except` block is skipped.
***
## Catching Specific Exceptions in Python
For each `try` block, there can be zero or more `except` blocks. Multiple `except` blocks allow us to handle each exception differently.
The argument type of each `except` block indicates the type of exception that can be handled by it. For example,
```
try:
even_numbers = [2,4,6,8]
print(even_numbers[5])
except ZeroDivisionError:
print("Denominator cannot be 0.")
except IndexError:
print("Index Out of Bound.")
# Output: Index Out of Bound
```
In this example, we have created a [list](https://www.programiz.com/python-programming/list) named even\_numbers.
Since the list index starts from **0**, the last element of the list is at index **3**. Notice the statement,
```
print(even_numbers[5])
```
Here, we are trying to access a value to the index **5**. Hence, `IndexError` exception occurs.
When the `IndexError` exception occurs in the `try` block,
- The `ZeroDivisionError` exception is skipped.
- The set of code inside the `IndexError` exception is executed.
***
## Python try with else clause
In some situations, we might want to run a certain block of code if the code block inside `try` runs without any errors.
For these cases, you can use the optional `else` keyword with the `try` statement.
Let's look at an example:
```
# program to print the reciprocal of even numbers
try:
num = int(input("Enter a number: "))
assert num % 2 == 0
except:
print("Not an even number!")
else:
reciprocal = 1/num
print(reciprocal)
```
**Output**
If we pass an odd number:
```
Enter a number: 1 Not an even number!
```
If we pass an even number, the reciprocal is computed and displayed.
```
Enter a number: 4 0.25
```
However, if we pass **0**, we get `ZeroDivisionError` as the code block inside `else` is not handled by preceding `except`.
```
Enter a number: 0 Traceback (most recent call last): File "<string>", line 7, in <module> reciprocal = 1/num ZeroDivisionError: division by zero
```
Here, the [assert](https://www.programiz.com/python-programming/assert-statement) statement in the code checks that `num` is an even number; if num is odd, it raises an `AssertionError`, triggering the except block.
**Note**: Exceptions in the `else` clause are not handled by the preceding except clauses.
***
## Python try...finally
In Python, the `finally` block is always executed no matter whether there is an exception or not.
The `finally` block is optional. And, for each `try` block, there can be only one `finally` block.
Let's see an example,
```
try:
numerator = 10
denominator = 0
result = numerator/denominator
print(result)
except:
print("Error: Denominator cannot be 0.")
finally:
print("This is finally block.")
```
**Output**
```
Error: Denominator cannot be 0. This is finally block.
```
In the above example, we are dividing a number by **0** inside the `try` block. Here, this code generates an exception.
The exception is caught by the `except` block. And, then the `finally` block is executed.
***
**Also Read:**
- [Python built-in Exception](https://www.programiz.com/python-programming/exceptions)
- [Python user-defined Exception](https://www.programiz.com/python-programming/user-defined-exception) |
| Shard | 35 (laksa) |
| Root Hash | 16786490165506891835 |
| Unparsed URL | com,programiz!www,/python-programming/exception-handling s443 |