Is there a standard defacto exception or the best way to pass "invalid input" information on an exception?

invalid_argumentis-a logic_error.

Both reference sites point to a string

This class [i.e. logic_error]defines the type of objects created as exceptions to report errors in the program’s internal logic, such as violation of logical premises or class invariants.

These errors are supposedly detected before the program runs.

Question: assuming that the semantics are invalid_argumentstrictly related to the “error of the programmers”, is there any de facto standard exception that the program / library / server can use for communication ++ at runtime for the external caller "the provided input is invalid"?
Does your previous experience have a regular pattern when using an exception of type invalid_input?
If so, is it a standard or does everyone just get their exceptions as needed?

Notes:

  • clearly stdexceptdoes not provide runtime semantics. And do not raise

  • / validation-as-early-as-possible, , ( ) : as - , , " ".


++ " " = , ( , ), ,

// fwd declaration
void function_facing_dirty_code(struct user_input& data);

void function_facing_the_user(const char* jsonArgs) {
  try {
    struct user_input;
    parse_user_input(json_args, user_input);
    function_facing_dirty_code(user_input);
  }
  catch(invalid_input& ii) {
    // **this** should be a standard error for erroneous input/args/etc
    // treat it by telling the out-of-my-control caller to behave
  }
  catch (std::runtime_exception& e) {
    // tell the caller: sorry, you've done nothing wrong,
    // but I'm having generic runtime troubles.
  }
}

void function_facing_dirty_code(struct user_input& data) {
  try {
    // ... do some work
    // ... do some more work
    // Ahhh
    throw std::invalid_argument("Requested amount over the daily redraw limit");
    // ooops. This will cause a BSoD instead of telling the user.
    // Because std::invalid_argument is a logic error
  }
  catch(std::logic_error& bsod) {
    // log an error, blame the author, snitch it to his boss,
    // then generate a BSoD for the user delight,
    // because she must NOT see our coding family dirty laundry
  }
}
+4
1

std::logic_error - ( vector::at - ? , , UB); invalid_argument - , IIRC std::bitset.

, " " "", "" ; : , .

<stdexcept> , , , . / ( ), , , , ; , ( ) (overflow/underflow/domain_error - , ?) (a out_of_range "", overflow - " ", ).

: stdexcept - , , , . , std::exception std::runtime_error, .

+3

Source: https://habr.com/ru/post/1652392/


All Articles