What type of exception should I throw?

后端 未结 5 1170
难免孤独
难免孤独 2020-12-08 03:40

After going through some links on exception handling (1, 2, and 3), I know that C++ programs can throw pretty much anything as exceptions (int, char*

5条回答
  •  自闭症患者
    2020-12-08 03:58

    Usually you should derive your own exception classes from std::exception and its derivatives to represent errors relevant to your application domain, for example if you deal with files you should have FileNotFoundException that includes the filepath and other relevant information, this way you can create catch blocks to handle certain error types and leave out others, you should also refrain from throwing or catching non-class exceptions.

    Have a look at similar exception hierarchies in .NET and Java to see how to model general errors(files errors, IO errors, network errors, etc)

提交回复
热议问题