Question

What is the purpose of the else block in exception handling?

Updated on May 31, 2025 | By Learnzy Admin | πŸ‘οΈ Views: 187 students

Solution
βœ” Verified

In Python, the else block in exception handling is used to define code that should run only if no exception was raised in the try block.

Purpose:

  • It separates the code that should only run when the try block is successful, making the intent clearer.
  • Helps in cleaner exception handling logic by keeping normal execution flow separate from error handling.

Example:--

try:
    result = 10 / 2
except ZeroDivisionError:
    print("Cannot divide by zero.")
else:
    print("Division successful. Result is:", result)
Was this solution helpful? 37
Click here to download practice questions on Exception Handling in Python

More Questions on Exception Handling in Python

Question 1

What is the use of raise in Python?


View solution
Question 2

How can you catch multiple exceptions in one block?


View solution
Question 3

What is the use of the finally block?


View solution
Question 4

Write the syntax of exception handling in Python.


View solution
Question 5

Name any four common exceptions in Python.


View solution
Question 6

"Every syntax error is an exception but every exception cannot be a syntax error." Justify the statement.


View solution