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

后端 未结 5 983
广开言路
广开言路 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:10

    I can't think of any obvious reasons why a class derived from std::exception wouldn't typically be better.

    But the throw 1 or throw "Error" are valid expressions, and they probably take a bit less "effort" creating, and there may be situations where this is a benefit. Throwing an exception type exception from the constructor of exception would probably not work very well, so there's one place.

    However, it's probably more of a philosophical decision: Since throw can be made to throw a any type of object, it's not a bad idea to allow it. The more restrictions you put on things in a language, the more you restrict the possible uses of a language.

提交回复
热议问题