ePrivacy and GPDR Cookie Consent by Cookie Consent

Raising and handling errors

yasmine provides an error handling mechanism for do behaviors. There are two variations available. One way is to attach an event to an exception that is thrown inside the do behavior. The other way is to attach an error event to a state.

Both the simple state and asynchronous simple state have the ability to store an event that is processed when an error occurs.

The error handling mechanism is triggered if an exception is thrown (and not caught) in the do behavior:

  • If the exception is of the type behavior_exception, the event stored in the exception will be extracted and passed to the state machine to be handled.

  • In case an std::exception is caught, the state machine checks if the state throwing the exception has an error event available. If it does, this event is passed to the state machine to be handled. If it does not, the state machine run will be aborted.

Behavior exception

Creation

A behavior exception (of type behavior_exception) can be created by passing a shared_ptr to an event through its constructor:

behavior_exception( const event_sptr _event )

How it works

When a behavior_exception is thrown in a do behavior, yasmine will catch it and then process the event that was stored in the exception object. If multiple do behaviors throw an exception, the error events will be handled one at a time sequentially.
The event transported by the behavior exception overrides the event stored in the state (if one is also stored there). This means that only one event will be processed.

State with error event

Creation

A state can store an event that is processed when an exception is raised during the do behavior execution of the state. For creating a state like that, the following constructor must be used:

explicit simple_state_impl( const std::string& _name, behavior_uptr do_action = behavior_uptr(),
behavior_uptr _entry_action = behavior_uptr(), behavior_uptr _exit_action = behavior_uptr(),
const event_ids& _deferred_events = event_ids(), event_sptr _error_event = event_sptr() );

How it works

When an exception that is derived from std::exception (but that is no behavior_exception) is thrown in a do-behavior, yasmine will catch it. yasmine will then check if the state, whose do-behavior threw the exception, has an error event attached to it. If it does, this event will to be processed. When there is no error event attached to this state, the exception will be re-thrown. This will cause the state machine run to be aborted and the state machine will thus stop.
If multiple do behaviors throw an exception, the error events will be handled one at a time sequentially.