---
title: Use Custom Error Classes
impact: HIGH
impactDescription: enables specific error handling and better logging
tags: error-handling, quality, python, pyspark
---

## Use Custom Error Classes

Custom error classes provide context and allow for granular error handling.

**Incorrect:**
```python
def process_spark(df):
    if df.count() == 0:
        raise Exception("Empty dataframe")
```

**Correct:**
```python
class EmptyDatasetError(Exception):
    """Raised when the input Spark DataFrame is empty"""
    pass

def process_spark(df):
    if df.count() == 0:
        raise EmptyDatasetError("Input orders table is empty")
```
