I want to skip the same error from different places. Here is an example:
int devide(int a,int b)
{
if(b==0) {
throw "b must be different from 0";
}
return 0;
}
int mod(int a,int b)
{
if(b==0) {
throw "b must be different than 0";
}
return 0;
}
And in the main function:
int main()
{
try {
devide(1,0);
} catch(char const* e) {
cout << e << endl;
}
try {
mod(1,0);
} catch(char const* e) {
cout << e << endl;
}
return 0;
}
Now the program output:
b must be different from 0
b must be different than 0
As you can see, the error is the same in nature, but the message is different. So what is the best practice that I can follow in this case so that it can be scalable? Let's say that I have 100 different types of exceptions, but when I throw an exception for devisor to be different from 0, I want to get the same messege wherever the exception is thrown.
EDIT:
I was thinking about two options:
- For each exception that I can throw, I will define my own class that inherits
std::exception. Each exception can be added to Exceptions.cpp. class MyException, std::exception, . - ( , ), , . , MyException, :
int devide(int a,int b)
{
if(b==0) {
throw new MyException(MyException::DEVISION_BY_ZERO);
}
}
MyException::DEVISION_BY_ZERO .
, , .
, aproach, ?