I'm working on a library that generates reports of devices. The generate_report (const std::string& no)
member function can fail due to various reasons:
- invalid report no.
- invalid state (the
report_generator
is a FSM) - no device is active
- error during report generation
Which error-handling mechanism is best for these errors?
- just return
true
orfalse
- return error code
- assert and log
- throw exception(s)
- any combination of the above
Some context information: the normal workflow is as following. The user activates a devices, chooses a report from a list and clicks on "generate".
EDIT: Thanks for the replies so far! For me it's clear now when to use asserts and when to do error-handling. As for error-handling, error codes and exceptions both have pros and cons. I think I go for exceptions (and create four classes for the above errors), but I'm not yet really convinced. I always thought of exceptions of 'unexpected situations'. An invalid report no isn't really unexpected. Any advice? :)
See Question&Answers more detail:os