Python Try...Except
Learn how to handle errors and exceptions in Python using try...except blocks.
Python Try Except
The try block lets you test a block of code for errors.
The except block lets you handle the error.
The else block lets you execute code when there is no error.
The finally block lets you execute code, regardless of the result of the try- and except blocks.
Exception Handling
When an error occurs, or exception as we call it, Python will normally stop and generate an error message.
These exceptions can be handled using the try statement:
Example - The try block will generate an exception, because x is not defined:
try:
print(x)
except:
print("An exception occurred") Since the try block raises an error, the except block will be executed.
Without the try block, the program will crash and raise an error.
Many Exceptions
You can define as many exception blocks as you want, e.g. if you want to execute a special block of code for a special kind of error:
Example - Print one message if the try block raises a NameError and another for other errors:
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong") Else
You can use the else keyword to define a block of code to be executed if no errors were raised:
Example - In this example, the try block does not generate any error:
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong") Finally
The finally block, if specified, will be executed regardless if the try block raises an error or not.
Example:
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished") This can be useful to close objects and clean up resources:
Example - Try to open and write to a file that is not writable:
try:
f = open("demofile.txt")
try:
f.write("Lorum Ipsum")
except:
print("Something went wrong when writing to the file")
finally:
f.close()
except:
print("Something went wrong when opening the file") Raise an Exception
As a Python developer you can choose to throw an exception if a condition occurs.
To throw (or raise) an exception, use the raise keyword.
Example - Raise an error and stop the program if x is lower than 0:
x = -1
if x < 0:
raise Exception("Sorry, no numbers below zero") The raise keyword is used to raise an exception.
You can define what kind of error to raise, and the text to print to the user.
Example - Raise a TypeError if x is not an integer:
x = "hello"
if not type(x) is int:
raise TypeError("Only integers are allowed") Common Exception Types
Python has many built-in exception types. Here are some common ones:
Catching Specific Exceptions
It's better to catch specific exceptions rather than using a bare except clause:
Example - Handling specific exceptions:
def divide_numbers(a, b):
try:
result = a / b
return result
except ZeroDivisionError:
print("Error: Cannot divide by zero!")
return None
except TypeError:
print("Error: Both arguments must be numbers!")
return None
except Exception as e:
print(f"An unexpected error occurred: {e}")
return None
# Test the function
print(divide_numbers(10, 2)) # 5.0
print(divide_numbers(10, 0)) # Error: Cannot divide by zero!
print(divide_numbers(10, "a")) # Error: Both arguments must be numbers! Getting Exception Information
You can get more information about the exception using the as keyword:
Example - Getting exception details:
def access_list_item(my_list, index):
try:
return my_list[index]
except IndexError as e:
print(f"Index error: {e}")
print(f"List length: {len(my_list)}, requested index: {index}")
return None
except TypeError as e:
print(f"Type error: {e}")
return None
# Test the function
numbers = [1, 2, 3, 4, 5]
print(access_list_item(numbers, 2)) # 3
print(access_list_item(numbers, 10)) # Index error
print(access_list_item(numbers, "a")) # Type error Custom Exceptions
You can create your own exception classes by inheriting from the Exception class:
Example - Custom exception classes:
class CustomError(Exception):
"""Base class for custom exceptions"""
pass
class ValidationError(CustomError):
"""Raised when input validation fails"""
def __init__(self, message, field_name=None):
self.message = message
self.field_name = field_name
super().__init__(self.message)
class BusinessLogicError(CustomError):
"""Raised when business logic rules are violated"""
pass
def validate_age(age):
"""Validate age input"""
if not isinstance(age, int):
raise ValidationError("Age must be an integer", "age")
if age < 0:
raise ValidationError("Age cannot be negative", "age")
if age > 150:
raise ValidationError("Age seems unrealistic", "age")
return True
def process_user_registration(name, age, email):
"""Process user registration with validation"""
try:
# Validate inputs
if not name or len(name.strip()) == 0:
raise ValidationError("Name is required", "name")
validate_age(age)
if "@" not in email:
raise ValidationError("Invalid email format", "email")
# Business logic validation
if age < 18:
raise BusinessLogicError("Users must be 18 or older to register")
print(f"User {name} registered successfully!")
return True
except ValidationError as e:
print(f"Validation Error in {e.field_name}: {e.message}")
return False
except BusinessLogicError as e:
print(f"Business Logic Error: {e}")
return False
except Exception as e:
print(f"Unexpected error: {e}")
return False
# Test the registration function
process_user_registration("John Doe", 25, "john@example.com") # Success
process_user_registration("", 25, "john@example.com") # Validation error
process_user_registration("Jane", 16, "jane@example.com") # Business logic error
process_user_registration("Bob", "thirty", "bob@example.com") # Validation error Exception Handling Best Practices
1. Be Specific with Exception Types
# Good - specific exception handling
try:
value = int(user_input)
result = 10 / value
except ValueError:
print("Please enter a valid number")
except ZeroDivisionError:
print("Cannot divide by zero")
# Avoid - too broad exception handling
try:
value = int(user_input)
result = 10 / value
except: # This catches ALL exceptions
print("Something went wrong") 2. Use Finally for Cleanup
def read_file_safely(filename):
file = None
try:
file = open(filename, 'r')
content = file.read()
return content
except FileNotFoundError:
print(f"File {filename} not found")
return None
except PermissionError:
print(f"Permission denied to read {filename}")
return None
finally:
if file:
file.close()
print("File closed") 3. Don't Ignore Exceptions
# Bad - silently ignoring exceptions
try:
risky_operation()
except:
pass # Don't do this!
# Good - at least log the exception
import logging
try:
risky_operation()
except Exception as e:
logging.error(f"Error in risky_operation: {e}")
# Handle appropriately Practical Examples
File Processing with Error Handling
import json
import os
def process_config_file(filename):
"""Process a JSON configuration file with comprehensive error handling"""
if not filename:
raise ValueError("Filename cannot be empty")
try:
# Check if file exists
if not os.path.exists(filename):
raise FileNotFoundError(f"Configuration file '{filename}' not found")
# Check file permissions
if not os.access(filename, os.R_OK):
raise PermissionError(f"No read permission for file '{filename}'")
# Read and parse the file
with open(filename, 'r') as file:
try:
config = json.load(file)
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON in {filename}: {e}")
# Validate required fields
required_fields = ['database_url', 'api_key', 'debug_mode']
missing_fields = [field for field in required_fields if field not in config]
if missing_fields:
raise KeyError(f"Missing required configuration fields: {missing_fields}")
# Validate data types
if not isinstance(config['debug_mode'], bool):
raise TypeError("debug_mode must be a boolean value")
print("Configuration loaded successfully!")
return config
except (FileNotFoundError, PermissionError, ValueError, KeyError, TypeError) as e:
print(f"Configuration error: {e}")
return None
except Exception as e:
print(f"Unexpected error while processing config: {e}")
return None
# Test the function
config = process_config_file("config.json")
if config:
print("Using configuration:", config) Network Request with Retry Logic
import time
import random
class NetworkError(Exception):
"""Custom exception for network-related errors"""
pass
class APIError(Exception):
"""Custom exception for API-related errors"""
pass
def simulate_api_call():
"""Simulate an API call that might fail"""
# Simulate random failures
if random.random() < 0.7: # 70% chance of failure
if random.random() < 0.5:
raise NetworkError("Connection timeout")
else:
raise APIError("Server returned error 500")
return {"status": "success", "data": "API response data"}
def make_api_request_with_retry(max_retries=3, delay=1):
"""Make API request with retry logic and exponential backoff"""
for attempt in range(max_retries + 1):
try:
print(f"Attempt {attempt + 1}/{max_retries + 1}")
result = simulate_api_call()
print("API call successful!")
return result
except NetworkError as e:
print(f"Network error: {e}")
if attempt < max_retries:
wait_time = delay * (2 ** attempt) # Exponential backoff
print(f"Retrying in {wait_time} seconds...")
time.sleep(wait_time)
else:
print("Max retries exceeded for network errors")
raise
except APIError as e:
print(f"API error: {e}")
# Don't retry on API errors (client errors)
raise
except Exception as e:
print(f"Unexpected error: {e}")
raise
# Test the retry logic
try:
response = make_api_request_with_retry(max_retries=3)
print("Final result:", response)
except (NetworkError, APIError) as e:
print(f"Request failed: {e}")
except Exception as e:
print(f"Unexpected error: {e}") Input Validation System
class InputValidator:
"""A comprehensive input validation system"""
@staticmethod
def validate_email(email):
"""Validate email format"""
if not isinstance(email, str):
raise TypeError("Email must be a string")
if not email or email.isspace():
raise ValueError("Email cannot be empty")
if "@" not in email or "." not in email:
raise ValueError("Invalid email format")
parts = email.split("@")
if len(parts) != 2:
raise ValueError("Email must contain exactly one @ symbol")
local, domain = parts
if not local or not domain:
raise ValueError("Email must have both local and domain parts")
return email.lower().strip()
@staticmethod
def validate_phone(phone):
"""Validate phone number"""
if not isinstance(phone, str):
raise TypeError("Phone number must be a string")
# Remove common separators
cleaned = phone.replace("-", "").replace("(", "").replace(")", "").replace(" ", "")
if not cleaned.isdigit():
raise ValueError("Phone number must contain only digits and separators")
if len(cleaned) < 10 or len(cleaned) > 15:
raise ValueError("Phone number must be between 10 and 15 digits")
return cleaned
@staticmethod
def validate_age(age):
"""Validate age"""
try:
age_int = int(age)
except (ValueError, TypeError):
raise ValueError("Age must be a valid integer")
if age_int < 0:
raise ValueError("Age cannot be negative")
if age_int > 150:
raise ValueError("Age must be realistic (0-150)")
return age_int
def collect_user_info():
"""Collect and validate user information"""
user_info = {}
# Collect email
while True:
try:
email = input("Enter your email: ")
user_info['email'] = InputValidator.validate_email(email)
break
except (ValueError, TypeError) as e:
print(f"Error: {e}. Please try again.")
# Collect phone
while True:
try:
phone = input("Enter your phone number: ")
user_info['phone'] = InputValidator.validate_phone(phone)
break
except (ValueError, TypeError) as e:
print(f"Error: {e}. Please try again.")
# Collect age
while True:
try:
age = input("Enter your age: ")
user_info['age'] = InputValidator.validate_age(age)
break
except ValueError as e:
print(f"Error: {e}. Please try again.")
return user_info
# Example usage (commented out for demo)
# print("Please provide your information:")
# user_data = collect_user_info()
# print("User information collected successfully:", user_data)