C++ 如何修改C++;运行时错误?
我有一个类继承自C++ 如何修改C++;运行时错误?,c++,exception,std,runtime-error,C++,Exception,Std,Runtime Error,我有一个类继承自std::runtime\u error,如下所示: #include <string> #include <stdexcept> class SomeEx : public std::runtime_error { public: SomeEx(const std::string& msg) : runtime_error(msg) { } }; 当然:SomeEx(unsigned int-id):运行时错误(std::to_str
std::runtime\u error
,如下所示:
#include <string>
#include <stdexcept>
class SomeEx : public std::runtime_error
{
public:
SomeEx(const std::string& msg) : runtime_error(msg) { }
};
当然:
SomeEx(unsigned int-id):运行时错误(std::to_string(id)){}
当然:SomeEx(unsigned int-id):运行时错误(std::to_string(id)){}
如果可以将数字转换为字符串,则可以简单地附加它们:
#include <string>
#include <stdexcept>
std::string BuildMessage(std::string const& msg, int x)
{
std::string result(msg);
// Build your string here
return result;
}
class SomeEx : public std::runtime_error
{
public:
SomeEx(const std::string& msg)
: runtime_error(BuildMessage(msg, id)) { }
};
#包括
#包括
std::string BuildMessage(std::string const&msg,intx)
{
std::字符串结果(msg);
//在这里建立你的字符串
返回结果;
}
类somex:public std::runtime\u错误
{
公众:
SomeEx(const std::string&msg)
:运行时_错误(BuildMessage(msg,id)){}
};
如果您可以将数字转换为字符串,则只需将其追加:
#include <string>
#include <stdexcept>
std::string BuildMessage(std::string const& msg, int x)
{
std::string result(msg);
// Build your string here
return result;
}
class SomeEx : public std::runtime_error
{
public:
SomeEx(const std::string& msg)
: runtime_error(BuildMessage(msg, id)) { }
};
#包括
#包括
std::string BuildMessage(std::string const&msg,intx)
{
std::字符串结果(msg);
//在这里建立你的字符串
返回结果;
}
类somex:public std::runtime\u错误
{
公众:
SomeEx(const std::string&msg)
:运行时_错误(BuildMessage(msg,id)){}
};
静态std::string get_消息(未签名的int-id){
std::stringstream-ss;
ssstatic std::string get_消息(无符号int-id){
std::stringstream-ss;
ss对Moing Duck的回答补充道,现在你可以使用lambda并直接这样称呼它:
SomeEx(unsigned int id) :
std::runtime_error {
[](const auto id) {
std::ostringstream ss;
ss << "invalid type ID " << id;
return ss.str();
}(id)
}
{
}
SomeEx(无符号int-id):
std::运行时错误{
[](常量自动id){
std::ostringstream ss;
ss对Moing Duck的回答补充道,现在你可以使用lambda并直接这样称呼它:
SomeEx(unsigned int id) :
std::runtime_error {
[](const auto id) {
std::ostringstream ss;
ss << "invalid type ID " << id;
return ss.str();
}(id)
}
{
}
SomeEx(无符号int-id):
std::运行时错误{
[](常量自动id){
std::ostringstream ss;
顺便说一句,这里的id不是一个错误id,它是一个特定于应用程序的id。但是,请参阅支持错误代码的参数!该博客似乎是“如何”的,乍一看,我没有看到任何支持使用错误代码的参数。大多数似乎是“如果我使用的是使用错误代码的库,那么如何将这些代码转换为正确的异常。”顺便说一句,这里的id不是错误id,而是特定于应用程序的id。但请参阅以获取支持错误代码的参数!该博客似乎是“如何”,一眼望去,我没有看到任何支持使用错误代码的论据。大多数论据似乎是“如果我使用的是使用错误代码的库,如何将这些代码转换为正确的异常。”