Errors & debugging
Handle failures gracefully, raise helpful errors, and debug with confidence.
Catch ValueError and print friendly message in Python
Wrap an int() call in a try/except block and print a friendly message when ValueError is raised.
try:
number = int("not_a_number")
except ValueError:
print("That's not a valid number. Please enter digits only.")
How to catch ValueError in Python and print a friendly message
This code defines a function that safely converts text to an integer, catches ValueError, and prints a friendly message instead of crashing.
def parse_number(text):
try:
return int(text)
except ValueError:
print("Oops! That's not a valid number.")
return None
if __name__ == "__main__":
result = parse_number("abc")
if result is None:
print("Parsing failed.")
else:
print(f"Parsed value: {result}")
Browse by section
Each section groups closely related Python snippets.
Errors & debugging — Python code examples
What you will find here
This page collects errors & debugging snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.