Oct-05-2019, 08:39 AM
(This post was last modified: Oct-05-2019, 08:40 AM by Gribouillis.)
The code that uses a function must take into account the specifications of that function. When you design the function, you need to say what the function does when it receives invalid parameters. What are the possibilities?
Note that there could be more exotic design option, for example the function could set a global indicator to say that it failed, but this is not relevant in a language that implements exceptions. It is the strategy used in the C implementation of python with the PyErr_SetString() and PyErr_Occurred() functions but that's another story.
- raise an exception (the type of the exception can be fine-tuned)
- return a value that indicates an error (can be None)
- return a dummy value
- return an unspecified value
- Ignore the error which includes letting an exception or a faulty value propagate
- Detect the error and take appropriate action
Note that there could be more exotic design option, for example the function could set a global indicator to say that it failed, but this is not relevant in a language that implements exceptions. It is the strategy used in the C implementation of python with the PyErr_SetString() and PyErr_Occurred() functions but that's another story.