Is there any advantages or uses cases to throw other thing that a std::exception( or a derivatives types).
For example throw 1;
or throw \"error\";
Yes, there can be advantages.
The most obvious is that the what
for existing derivatives of std::exception
(e.g., std::logic_error
, std::invalid_argument
) only deal with std::string
s or char *
s to specify the reason for the exception. That's probably fine if you only deal with an English speaking audience, but if you want to use the what
to display an error message to somebody who doesn't use English, it can get clumsy in a hurry.
If you're using (for example) 32-bit Unicode strings throughout your program, you'd typically prefer do the same in your exception handling. In such a case, a hierarchy similar to those in the standard, but using 32-bit Unicode strings for the what
argument probably makes a lot of sense. I suppose you could still use std::exception
for the base of that hierarchy, but it's open to question how much (if anything) you'd gain from doing so.