I can't quite wrap my head around how a user will be able to distinguish between the exceptions my functions can throw. One of my functions can throw two instances of std::invalid_argument
.
For example:
#include <stdexcept> // std::invalid_argument
#include <string>
void foo(int hour, int minute)
{
if(hour < 0 || hour > 24)
throw std::invalid_argument(std::string("..."));
if(minute <0 || minute > 59)
throw std::invalid_argument(std:string("..."));
}
Note: It's an example, please do not answer with bounded integers.
Say the user calls with foo(23, 62);
, how would the user's exception handler distinguish between the two possible instances of std::invalid_argument
?
Or am I doing this wrong, and I should inherit from `std::invalid_argument to distinguish between them? That is,
class InvalidHour: public std::invalid_argument
{
public:
InvalidHour(const std::string& what_arg)
:std::invalid_argument(msg) {};
}
class InvalidMinute: public std::invalid_argument
{
public:
InvalidMinute(const std::string& what_arg)
:std::invalid_argument(msg) {};
}
Then, throw InvalidHour
and InvalidMinute
?
Aucun commentaire:
Enregistrer un commentaire