Raising Exceptions (The raise Keyword)
So far, we have been catching errors that Python raises automatically. But sometimes, you want to trigger an error manually when a specific rule in your program is violated.
For example: A user enters -5 as their age. Python does not see any problem with this — it is a valid integer. But your application's rules say age must be positive.
This is where the raise keyword comes in.
Basic Syntax
raise ExceptionType("Your custom error message")
Example: Validating Age
def check_age(age):
if age < 0:
raise ValueError("Age cannot be negative!")
print(f"Age {age} is valid.")
try:
check_age(-5)
except ValueError as e:
print("Caught error:", e)
# Output: Caught error: Age cannot be negative!
When Should You Use raise?
Use raise when your code receives technically valid data that violates your business rules:
| Scenario | Python's View | Your Rule |
|---|---|---|
age = -5 | Valid integer ✅ | Age must be positive ❌ |
password = "hi" | Valid string ✅ | Must be 8+ characters ❌ |
quantity = 0 | Valid number ✅ | Must order at least 1 item ❌ |
Re-raising an Exception
Sometimes you want to catch an error, log it, and then let it crash anyway so the caller knows something went wrong. Use raise without arguments inside an except block:
def process_payment(amount):
try:
if amount <= 0:
raise ValueError("Payment must be positive!")
print(f"Processing ₹{amount}...")
except ValueError as e:
print(f"[LOG] Payment error: {e}")
raise # Re-raise the same error to the caller
try:
process_payment(-100)
except ValueError:
print("Transaction failed.")
Output:
[LOG] Payment error: Payment must be positive!
Transaction failed.
Quick Summary
raiseKeyword: Manually throws an exception when application rules or business logic constraints are violated.- Syntax:
raise ExceptionType("Custom explanation message"). - Re-raising Exceptions: Writing
raisewithout arguments inside anexceptblock logs or catches the issue locally and propagates it up to the caller.
What's Next?
Let's learn how to create your own domain-specific error classes by building Custom Exceptions in the next lesson!