我目前正在项目中实现一些自定义异常,无法决定对我的异常进行typedef或为每个异常派生一个新类。对每个人的潜在利弊感兴趣,如果有人更喜欢?typedef std :: runtime_error MyError vs class MyError:public std :: runtime_error
3
A
回答
3
派生你自己班的优点很简单:你可以在catch
处理程序中派发它。
如果你决定不实现你自己的类,我仍然怀疑你需要这个typedef:你从中抽象出什么?你也可以使用普通的std::runtime_error
。
另外你可能会对David Abrahams'es关于异常处理和实现你自己的异常类的文章感兴趣。
2
派生一个新的类 - 那么你可以区分它们之间。如果你使用typedef,异常处理程序就无法知道throw中使用了哪个typedef。
0
我有点难以理解你的问题,但是如果你正在制作你自己的自定义异常,我想我会使用派生类来防止后来改变行为。
相关问题
- 1. 区别:std :: runtime_error vs std :: exception()
- 2. 如何从std :: runtime_error继承?
- 3. Qt std :: runtime_error相当于
- 4. string&vs. wstring&in runtime_error
- 5. 误解std :: runtime_error的什么()函数
- 6. gcc 4.9.1不符合标准? (std :: runtime_error)
- 7. 使用std :: runtime_error而不是CException
- 8. 缺少的std :: runtime_error在QT + MinGW的
- 9. 为什么std :: runtime_error :: what()返回const char *而不是std :: string const&
- 10. C++ 0x random_device'std :: runtime_error'
- 11. 从性病继承:: runtime_error
- 12. 试图从STD继承时编译错误:: runtime_error
- 13. 上终止实例 “的std :: runtime_error” 街舞的PHP
- 14. 这个C++异常语法(std :: runtime_error)有什么问题?
- 15. 为什么创建一个std :: runtime_error重置WSAGetLastError?
- 16. C++:Catch runtime_error
- 17. runtime_error期待一个“;”?
- 18. 如何修改C++ runtime_error的字符串?
- 19. std :: vector vs std :: insert
- 20. std :: mem_fun vs std :: mem_fn
- 21. std :: unique_ptr :: release()vs std :: move()
- 22. std :: move VS std ::转发
- 23. std :: list vs std :: vector迭代
- 24. Functors vs. std :: bind
- 25. 错误:在'class std :: result_of
- 26. std :: throw_with_nested()在内存不足条件调用std :: terminate()
- 27. snprintf vs std :: stringstream
- 28. 的std ::删除VS的std ::的remove_if
- 29. Barton-Nackman vs std :: enable_if
- 30. 创建内联std :: string