Skip to content

Instantly share code, notes, and snippets.

@roganjoshp
Last active February 23, 2026 15:36
Show Gist options
  • Select an option

  • Save roganjoshp/0ae629559cf3d438c10422a1aafc6963 to your computer and use it in GitHub Desktop.

Select an option

Save roganjoshp/0ae629559cf3d438c10422a1aafc6963 to your computer and use it in GitHub Desktop.
Custom exceptions in a library
# exceptions.py
class ProblemError(Exception):
"""An exception raised during problem specification"""
class ShiftError(ProblemError):
"""Some aspect of the shift pattern definition is invalid"""
# shifts.py (collected exceptions from the module)
raise ShiftError("No hours are registered for this shift")
raise ShiftError("The shift day does not start at midnight")
raise ShiftError(
(
"Non-contiguous shift block encountered between: "
f"{self.periods[i]} and {self.periods[i + 1]}"
).lstrip()
)
raise ShiftError(
(
"The following shift does not run until midnight: "
f"{self.periods[-1]['end']}"
).lstrip()
)
raise ShiftError(
(
"The date of the first shift activity of this pattern "
"does not match the reference start date. If no shift "
"activity is planned for this day, use `add_downday()`"
" to fill in any gaps."
).lstrip()
)
raise ShiftError("Shift pattern is not contiguous in days")
raise ShiftError("Shift pattern has not been built")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment