Is there any advantages to throw other thing that a std::exception( or derivatives types)

后端 未结 5 985
广开言路
广开言路 2021-01-21 02:25

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\";

5条回答
  •  走了就别回头了
    2021-01-21 03:01

    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::strings 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.

提交回复
热议问题