An exception is an error that occurs during the execution of a program. When an error occurs, Python stops and generates an error message. You can handle exceptions using try, except, else, and finally blocks to keep the program running.
tryblock: Tests a block of code for errors.exceptblock: Handles the error if one occurs.
try:
number = int(input("Enter a number: "))
result = 10 / number
print(result)
except ZeroDivisionError:
print("Error: Cannot divide by zero!")You can catch different exceptions together using parentheses.
try:
value = int("abc")
except (TypeError, ValueError) as e:
print(f"Caught an error: {e}")Use the as keyword to inspect the exception object and print its details.
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"Error details: {e}") # Output: Error details: division by zeroelse: Runs code if no exception occurred in thetryblock.finally: Always runs regardless of whether an exception was raised.
try:
print("Running process...")
except ZeroDivisionError:
print("Error occurred")
else:
print("Process succeeded with no errors!")
finally:
print("Cleaning up resources.") # This will run no matter whatYou can manually trigger exceptions using the raise keyword. You can also create custom exception classes by inheriting from the built-in Exception class.
# Custom exception class
class InsufficientFundsError(Exception):
"""Raised when a bank withdrawal exceeds the available balance."""
def __init__(self, balance, amount):
self.balance = balance
self.amount = amount
super().__init__(f"Cannot withdraw {amount}. Balance is only {balance}.")
def withdraw(balance, amount):
if amount > balance:
raise InsufficientFundsError(balance, amount)
return balance - amount
try:
withdraw(100, 200)
except InsufficientFundsError as e:
print(e) # Output: Cannot withdraw 200. Balance is only 100.| Exception | When it occurs |
|---|---|
ValueError |
Right type but wrong value (e.g., int("abc")). |
TypeError |
Operation on incompatible types (e.g., "a" + 1). |
IndexError |
Accessing a sequence index that doesn't exist. |
KeyError |
Accessing a dictionary key that doesn't exist. |
ZeroDivisionError |
Dividing a number by zero. |
FileNotFoundError |
Trying to open a file that does not exist. |
AttributeError |
Accessing an attribute that doesn't exist on an object. |
ImportError |
Importing a module that doesn't exist. |
Using except: without specifying an exception class catches all exceptions, including system-exiting exceptions like SystemExit and KeyboardInterrupt (when you try to stop execution using Ctrl+C). This makes debugging extremely hard.
# Incorrect:
try:
# Some code
pass
except:
print("An error occurred") # Catches EVERYTHING, even system interrupts!
# Correct:
try:
# Some code
pass
except Exception as e:
print(f"An unexpected error occurred: {e}") # Catches standard exceptions only