Exception Handling in Python in Python

Updated on May 31, 2025 | By Learnzy Academy

Exception Handling is used to manage errors during program execution without crashing the program.

Common Keywords:

  1. try – Block to test for errors.
  2. except – Block to handle the error.
  3. else – Runs if no error occurs.
  4. finally – Always runs, error or not.

Example:-

try:
    x = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero.")
else:
    print("Division successful.")
finally:
    print("Execution complete.")

Output:--

Cannot divide by zero.
Execution complete.
Click here to download practice questions on Exception Handling in Python