How to Throw Errors in Python
Python uses the raise keyword to “throw” exceptions.
What you’ll build or solve
You’ll raise clear, useful errors in Python so your code fails fast instead of producing confusing results.
Learn Python on Mimo
When this approach works best
This approach works best when you:
- Validate user input or function arguments and want to stop early when data is invalid.
- Guard against impossible states, like negative values that should never happen.
- Build reusable functions and want callers to see a clear error instead of silent bugs.
Avoid this approach when:
- “No result” is expected and normal, and returning
None(or an empty list) is clearer than raising.
Prerequisites
- Python 3 installed
- You know what variables are
- You can write and call a function
Step-by-step instructions
1) Raise an error with raise
Use raise to stop execution and signal that something is wrong.
age=-3
ifage<0:
raiseValueError("age must be 0 or higher")
What to look for:
Write a message that clearly states what was expected.
2) Pick a standard exception type
Choose an exception that matches the problem. This makes errors easier to understand and easier to handle later.
Option A (most common): use ValueError for invalid values
defset_discount(percent):
ifpercent<0orpercent>100:
raiseValueError("percent must be between 0 and 100")
returnpercent
Option B (alternative): use TypeError for invalid types
defrepeat(text,times):
ifnotisinstance(times,int):
raiseTypeError("times must be an int")
returntext*times
3) Add context to your error message
If the caller passed a bad value, include it in the message.
CSS
defget_user(user_id):
ifuser_id<=0:
raiseValueError(f"user_id must be positive, got{user_id}")
return {"id":user_id}
What to look for:
Keep the message short, but include the rule and the value.
4) Create your own exception type for a specific domain
Most code should stick to built-in exceptions. In larger projects, a custom exception can make your errors easier to catch and understand.
classPaymentError(Exception):
pass
defcharge(amount):
ifamount<=0:
raisePaymentError("amount must be greater than 0")
return"charged"
Examples you can copy
1) Validate function arguments
defslugify(text):
ifnotisinstance(text,str):
raiseTypeError("text must be a string")
ifnottext.strip():
raiseValueError("text must not be empty")
returntext.strip().lower().replace(" ","-")
2) Guard against missing required keys
This assumes user is a dictionary, like {"email": "a@b.com"}.
defget_email(user):
if"email"notinuser:
raiseKeyError("user must include an 'email' key")
returnuser["email"]
3) Stop on an unexpected state
defstatus_label(status):
allowed= {"todo","doing","done"}
ifstatusnotinallowed:
raiseValueError(f"status must be one of{sorted(allowed)}, got{status!r}")
returnstatus.upper()
4) Advanced: re-raise with a clearer message
Use this pattern when you want to convert a low-level error into a more helpful message for your app.
defparse_port(text):
try:
port=int(text)
exceptValueErrorase:
raiseValueError(f"port must be an integer, got{text!r}")frome
ifnot (1<=port<=65535):
raiseValueError(f"port must be 1..65535, got{port}")
returnport
The from e keeps the original error context in the traceback.
Common mistakes and how to fix them
Mistake 1: Raising a string instead of an exception
You might write:
Python
raise"Bad input"
Why it breaks:
Python requires an exception object, not a plain string.
Correct approach:
raiseValueError("bad input")
Mistake 2: Raising Exception for everything
You might write:
defset_age(age):
ifage<0:
raiseException("bad age")
Why it breaks:
A generic exception type makes the error less clear and harder to handle selectively.
Correct approach:
defset_age(age):
ifage<0:
raiseValueError("age must be 0 or higher")
returnage
Mistake 3: Not including a useful message
You might write:
raiseValueError()
Why it breaks:
The traceback shows the exception type but gives no clue what went wrong.
Correct approach:
raiseValueError("expected a number between 1 and 10")
Troubleshooting
If you see TypeError: exceptions must derive from BaseException, you raised something that is not an exception class or instance. Use raise ValueError("..."), not raise "...".
If you see NameError: name 'ValueError' is not defined, a variable named ValueError may be overriding the built-in. Rename your variable and restart Python.
If you see a traceback you do not understand, read the last line first. That line shows the exception type and your message.
Quick recap
- Python “throws” errors with the
raisekeyword. - Use
ValueErrorfor invalid values andTypeErrorfor invalid types. - Add a short message that states the rule and, when helpful, the bad value.
- Use a custom exception only when your project benefits from domain-specific errors.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot