抛出C ++异常时抛出什么? [英] What to throw when throwing C++ exceptions?

查看:113
本文介绍了抛出C ++异常时抛出什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但在C ++中,当我想抛出一个异常..我该抛什么?

This might be kind of a silly question, but in C++, when I want to throw an exception.. what do I throw?

我应该抛出std :: exception,还是由标准库保留的?或者我应该抛出一个字符串或int?

Am I supposed to throw std::exception, or is that reserved by the standard library? Or should I throw a string or int? Or should I just throw whatever I feel is appropriate?

推荐答案

抛出一个源自 std的类: :exception ;如果您 #include< stdexcept> ,您可以从一些

Throw a class that's derived from std::exception; if you #include <stdexcept>, you can pick from a number of ready-made, useful derived classes.

派生自 std :: exception 允许你的处理程序遵循可识别的风格,因为你总是可以使用 .what()来获取文本消息。不要抛出原始类型,因为它们不带有语义信息。

Deriving from std::exception allows your handlers to follow a recognizable style, as you can always use .what() to get a textual message. Don't throw primitive types, since they carry no semantic information.

这篇关于抛出C ++异常时抛出什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆