mercredi 25 mars 2015

What's the best way to handle slightly different exceptions?


My code right now looks something like this:



void throw_illegal_part_of_input_exception(char c) {}
void throw_invalid_input_length_exception(int position, int length) {}
void throw_invalid_subinput_length_exception(int position, int length, bool is_greater) {}
// Some other similar functions

// ... do some stuff inside a function
if (errorCondition1) {
throw_invalid_character_exception(c);
} else if (errorCondition2) {
throw_invalid_input_length_exception(position, length);
} // etcetera


The content of the functions is largely the same; however, the error messages I want to use in the thrown exceptions are very different and depend on the arguments. Is there a better way to write this?





Aucun commentaire:

Enregistrer un commentaire