Part 3: Runtime and Arithmetic Errors

Introduction

At 2 AM, I received an alert: my application had crashed. The logs showed ZeroDivisionError: division by zero. The code had been running fine for weeks, but a user had entered unexpected data, and suddenly everything stopped. This was my crash course in runtime errors—bugs that only appear when code is actually running, often with specific inputs or conditions.

Runtime and arithmetic errors are particularly challenging because they can hide during development and testing, only to emerge in production with real-world data.

Runtime Errors

What Are Runtime Errors?

Runtime errors occur during program execution when something unexpected happens that the code can't handle. Unlike syntax errors caught before execution, runtime errors only manifest when specific code paths are executed with particular data.

Real-World Examples from My Projects

Division by Zero

From a reporting dashboard I built:

# Incorrect - No handling for zero division
def calculate_success_rate(successful_requests, total_requests):
    """Calculate request success rate as percentage"""
    success_rate = (successful_requests / total_requests) * 100
    return success_rate

# Crashes when no requests made
rate = calculate_success_rate(0, 0)  # ZeroDivisionError!

Error:

Correct version:

Type Errors with User Input

From a CLI tool I developed:

Correct version:

Index Out of Range

From a log parser:

Correct version:

Key Errors with Dictionaries

From a REST API client:

Correct version:

File Not Found Errors

From a configuration loader:

Correct version:

How I Prevent Runtime Errors

1. Input Validation

2. Defensive Programming with Try-Except

3. Use Context Managers for Resources

Arithmetic Errors

What Are Arithmetic Errors?

Arithmetic errors occur during mathematical operations, including division by zero, overflow/underflow, precision loss, and incorrect operator usage.

Real-World Examples

Floating-Point Precision Issues

From a financial calculation module:

Correct version:

Integer Overflow (Less Common in Python)

Modulo Operation with Zero

Correct version:

Incorrect Rounding

From a pricing calculator:

Correct version:

How I Prevent Arithmetic Errors

1. Use Appropriate Data Types

2. Validate Before Operations

3. Handle Edge Cases

Practical Testing Strategies

Unit Tests for Runtime Scenarios

Property-Based Testing

Tools I Use

Error Tracking

Logging

Key Takeaways

  1. Validate all inputs: Never trust user data or external sources

  2. Handle exceptions explicitly: Catch specific exceptions, not generic Exception

  3. Use appropriate data types: Decimal for money, int for counts

  4. Test edge cases: Empty lists, zero values, None, negative numbers

  5. Log errors properly: Include context for debugging

  6. Fail fast: Validate early, before processing begins

  7. Use context managers: Ensure resources are cleaned up

Next in Series

In Part 4: Resource and Performance Errors, we'll explore errors related to system resources—memory leaks, file handle exhaustion, and performance bottlenecks that can bring your application to its knees under load.


Based on debugging production incidents in payment processing systems, API services, and data analytics platforms.

Last updated