Boost logo

Boost :

Subject: [boost] Exceptions vs Assertion for library API?
From: Jonathan Biggar (jon_at_[hidden])
Date: 2017-11-11 01:53:55

Any advice in choosing between throwing an exception vs asserting for
invalid uses of a library API?

I'm building a small "transactional" library I'd like to contribute to
boost where certain API calls are constrained to only be valid if the
transaction is still "in-progress". Such calls will never succeed, and are
easy for the implementation to diagnose. I'd like to have the library fail
an assertion, which is far clearer to the user that the code logic is
incorrect, but it bothers me that assertions are much more difficult to
write unit tests for. (Is there even a concept of a test that passes if if
fails to compile or fails with an assertion in the boost testing framework?)

I suppose I could mangle the interface to ensure that the user has nothing
to invoke the API calls in question unless the transaction is in-progress,
but that generally requires a custom "lock" interface for my RAII
implementation that makes the API more complex to grasp.

Jon Biggar

Boost list run by bdawes at, gregod at, cpdaniel at, john at