dimanche 1 décembre 2019

Custom exception in C++ for invalid input to a function

I would like to write a custom Error class in C++. I am mostly used to Java (not used them a lot) so I would like to check here if my thought process on how to do this in C++ is correct.

Currently I got the following code:

class InvalidInput : public std::runtime_error {
    InvalidInput(const char* msg) : std::runtime_error(msg) {

    }
};

I plan on using the cutsom error in a function as so:

myFunc(int x) {
    if (valueIsInvalid(x)) {
        throw InvalidInput ("invlaid input");
    }
}

Before implementing it I would like to know if I am on the right track on how this should be done in C++ (as well as best practice). If there is a better way feel free to tell me as well.

Aucun commentaire:

Enregistrer un commentaire