PyBiorythm uses a structured exception hierarchy for comprehensive error handling.
Author: Peter Rosemann (dkdndes@gmail.com)
BiorhythmError (base)
├── DateValidationError
├── ChartParameterError
└── CalculationError
Base exception for all biorhythm-related errors.
class BiorhythmError(Exception):
"""Base exception for all biorhythm errors."""
pass
Date validation errors for invalid dates or date ranges.
class DateValidationError(BiorhythmError):
"""Raised for invalid date inputs."""
pass
Common scenarios:
Chart configuration errors for invalid parameters.
class ChartParameterError(BiorhythmError):
"""Raised for invalid chart parameters."""
pass
Common scenarios:
Calculation errors during biorhythm computation.
class CalculationError(BiorhythmError):
"""Raised for calculation failures."""
pass
from biorythm import BiorhythmCalculator, DateValidationError
from datetime import datetime
try:
calc = BiorhythmCalculator()
# Future date will raise error
future_date = datetime(2030, 1, 1)
result = calc.calculate_biorhythm_values(future_date, datetime.now())
except DateValidationError as e:
print(f"Date error: {e}")
# Handle invalid date
from biorythm import BiorhythmCalculator, ChartParameterError
try:
# Width too small
calc = BiorhythmCalculator(width=5)
except ChartParameterError as e:
print(f"Parameter error: {e}")
# Use default parameters
calc = BiorhythmCalculator()
from biorythm import BiorhythmCalculator, BiorhythmError
try:
calc = BiorhythmCalculator()
# ... operations
except BiorhythmError as e:
print(f"Biorhythm error: {e}")
# Handle any biorhythm-related error
except Exception as e:
print(f"Unexpected error: {e}")
# Handle other errors
# Good - specific handling
try:
calc.generate_chart(birthdate)
except DateValidationError:
# Handle date errors specifically
show_date_input_form()
except ChartParameterError:
# Handle parameter errors
use_default_parameters()
import logging
logger = logging.getLogger(__name__)
try:
calc.generate_chart(birthdate)
except BiorhythmError as e:
logger.error(f"Biorhythm calculation failed: {e}")
raise
try:
calc = BiorhythmCalculator(width=user_width)
except ChartParameterError as e:
# Convert technical error to user message
print(f"Chart width must be at least 12 characters. Please try again.")
"Birth date cannot be in the future"
"Year must be between 1 and 9999"
"Invalid date: 2024-02-30"
"Month must be between 1 and 12"
"Day must be between 1 and 31"
"Chart width must be at least 12 characters"
"Invalid orientation: must be 'vertical' or 'horizontal'"
"Number of days must be positive"
"Calculation failed for date range"
"Mathematical overflow in cycle computation"
Related: Core API | Calculator API | JSON Schema |