Standardize Exception Hierarchy across Core Modules
Description
Following the successful Proof of Concept in #3197, which introduced a structured exception hierarchy for the discrete_space module, we propose expanding this standard to the rest of the Mesa core modules (Model, Time, AgentSet, and Visualization).
Currently, many core components raise generic Python exceptions like ValueError, TypeError, or KeyError. This makes it difficult for users to catch framework-specific errors without risk of accidentally suppressing unrelated system or logic errors.
Proposed Hierarchy Expansion
I suggest adding the following base classes and specific subtypes to mesa/exceptions.py:
ModelException: For model initialization and lifecycle errors.- RNGMismatchException(ModelException, ValueError): Raised when rng and scenario.rng do not match.
TimeException: For scheduling and event-related errors.- PastEventException(TimeException, ValueError): Replaces ValueError when scheduling events in the past.
- InvalidCallbackException(TimeException, TypeError): For invalid (non-callable) event functions.
- InvalidScheduleException(TimeException, ValueError): For invalid intervals or start times.
AgentException: For registration and lifecycle errors.- AgentNotRegisteredException(AgentException, LookupError)
- DuplicateAgentIDException(AgentException, KeyError)
AgentSetException: For errors in vectorized operations.- InvalidOptionException(AgentSetException, ValueError): e.g., for handle_missing validation.
VisualizationException: For rendering and backend errors.- UnsupportedBackendException(VisualizationException, ValueError)
Backward Compatibility
To ensure this is a non-breaking change for users already catching generic exceptions, all new specific exceptions will use multiple inheritance (e.g., class PastEventException(TimeException, ValueError)). This follows the pattern established with DimensionException in PR #3197.
Rationale
Standardizing these exceptions allows for cleaner, safer error-handling code in complex simulations and provides better diagnostic information for Mesa users.
@falloficaruss saw your note on #3384 about doing this one exception at a time, so here's a small one that shouldn't overlap with the ModelException work.
AgentSet.remove() on an agent that isn't in the set lets the KeyError from the internal WeakKeyDictionary escape, so the error you get is just:
KeyError: <weakref at 0x118db9a80; to 'Agent' at 0x118d496d0>
model.agents.remove(...) / agents_by_type[...] give KeyError: <Agent id=4>, a bit better but still not very helpful.
We already have AgentMissingException for the cell version of this, so I'd reuse it rather than add anything new:
AgentSet.removeand_HardKeyAgentSet.removecatch the KeyError andraise AgentMissingException(agent) from eAgentMissingExceptionalso subclassesKeyError, soMutableSet.removesemantics stay the same and thesuppress(KeyError)inAgent.removekeeps workingcoordinatebecomes optional, and without it the message readsAgent 4 is not in the AgentSet.
I have it working locally, and the existing tests that expect KeyError pass unchanged. Does this fit the gradual approach? If so I'll open the PR.