I have taken it upon me to modernise a few libraries from Netlib: use modern language constructions, provide a more modern interface. One thing I am struggling with is the way to handle errors. Most such libraries provide subroutines with some argument that indicates an error or warning. Quite often they also print to “standard output” to indicate what was wrong.
I prefer to have a clean interface:
- Functions return a value (scalar or if necessary a derived type or an array) but do not change their arguments (no output arguments).
- Output to screen/standard output gives trouble if you call the function in a write statement, so they should be avoided. But you also want the user to be able to recover from them, so an error stop in case of a more or less serious error is out of the question.
- Providing information about an unusual condition - for instance in the case of interpolation over two-dimensional data points, you may want to know if extrapolation occurred. But you may not necessarily want to check such a condition with each call to the interpolation function.
- The library could store a status variable that can cleared and queried, but that is not thread-safe per se.
My question is: what is a good strategy?
It is difficult to imagine a single solution will fit all situations, but maybe two or more methods could do.