Logo

Programming-Idioms

  • Ruby
  • C++

Idiom #89 Handle invalid argument

You've detected that the integer value of argument x passed to the current function is invalid. Write the idiomatic way to abort the function execution and signal the problem.

throw domain_error("oops!");
raise ArgumentError, "invalid value #{x}."

The error-message will automatically be enriched with filename, methodname and linenumber.
enum {
    E_OK,
    E_OUT_OF_RANGE
};

int square(int x, int *result) {
    if (x > 1073741823) {
        return E_OUT_OF_RANGE;
    }
    *result = x*x;
    return E_OK;
}

New implementation...
< >
programming-idioms.org