Reference library

Errors & debugging

Handle failures gracefully, raise helpful errors, and debug with confidence.

5 matches
Errors & debugging easy

How to Handle ValueError When Converting Strings to Integers in Python

Convert text to an integer with a try-except block that catches ValueError and prints beginner-friendly error messages.

valueerror try-except int
Python
def parse_number(text):
    """Convert text to an integer, showing beginner-friendly error handling."""
    try:
        number = int(text)
        print(f"Successfully parsed: {number}")
        return number
    except ValueError as e:
        print(f"Error: '{text}' is not a valid number.")
        print(f"Debuggin…
11 0 Open
Errors & debugging easy

How to Handle ValueError and Multiple Exceptions in Python

This code demonstrates try/except blocks for beginners, handling ZeroDivisionError, TypeError, and ValueError with two practical functions: dividing numbers and parsing strings to floats.

try-except valueerror exception-handling
Python
def divide_numbers(a, b):
    """Divide two numbers with error handling for beginners."""
    try:
        result = a / b
        print(f"{a} / {b} = {result}")
        return result
    except ZeroDivisionError:
        print(f"Error: Cannot divide {a} by zero!")
    except TypeError:
        print(f"Error: Both argu…
13 0 Open
Errors & debugging easy

How to Use try except ValueError in Python to Parse Numbers

Convert strings to integers safely with try/except ValueError and TypeError, returning a value-or-error tuple.

try-except valueerror error-handling
Python
def parse_number(text):
    """Safely convert a string to an integer, handling errors gracefully."""
    try:
        value = int(text)
        return value, None
    except ValueError as error:
        return None, f"Conversion failed: {error}"
    except TypeError as error:
        return None, f"Wrong type provided…
13 0 Open
Errors & debugging easy

How to Validate JSON in Python and Catch JSONDecodeError

A robust Python function that attempts to parse JSON strings and returns a boolean plus either the parsed data or a descriptive error message when decoding fails.

json validation jsondecodeerror
Python
import json

def validate_json(json_string):
    """Try to parse JSON, return (is_valid, data_or_error)."""
    try:
        data = json.loads(json_string)
        return True, data
    except json.JSONDecodeError as e:
        return False, f"Invalid JSON: {e}"

if __name__ == "__main__":
    test_inputs = [
        …
11 0 Open
Errors & debugging easy

Try Except ValueError in Python: Handle Conversion Errors

Catch ValueError exceptions when converting strings to integers or performing arithmetic, returning None on failure instead of crashing.

try-except valueerror exception
Python
def convert_to_int(value):
    try:
        return int(value)
    except ValueError as error:
        print(f"Conversion failed: {error}")
        print(f"Problem value was: {repr(value)}")
        return None


def divide_numbers(numerator, denominator):
    try:
        result = numerator / denominator
        retur…
13 0 Open

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.