从std :: exception的what返回一个动态字符串 [英] Return a dynamic string from std::exception's `what`

查看:180
本文介绍了从std :: exception的what返回一个动态字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这一点上,我确信我应该为所有异常抛出需求创建 std :: exception 的子类。现在,我正在研究如何覆盖什么方法。

I'm convinced at this point that I should be creating subclasses of std::exception for all my exception throwing needs. Now I'm looking at how to override the what method.

我所面临的情况如果字符串 what 返回的是动态的,将非常方便。例如,一些代码片段解析XML文件,并在错误消息中添加位置或行号对我很有用。

The situation that I'm facing, it would be really handy if the string what returns be dynamic. Some pieces of code parse an XML file for example, and adding a position or line number to the error message is useful to me.

我试图遵循提升异常处理指南

什么我想知道:


  • 什么返回 const char * ,这意味着任何捕获程序都可能不会释放该字符串。所以我需要其他地方存储结果,但是那应该在哪里呢? (我需要线程安全。)

  • what returns a const char *, which implies any catcher is likely not going to free the string. So I need some other place to store the result, but where would that be? (I need thread-safety.)

什么还包括 throw( )签名。虽然我可以防止什么抛出任何东西,但在我看来,这种方法的确不是针对动态性太强的对象。如果什么不合适,那么我应该在哪里呢?

what also includes throw() in its signature. While I can prevent my what from throwing anything, it seems to me that this method really isn't intended for anything too dynamic. If what is not the right place, then where should I be doing this instead?

从到目前为止的答案来看,似乎完成此操作的唯一方法是将字符串存储在异常中。 Boost指南建议不要这样做,这使我感到困惑,因为 std :: runtime_error 就是这样做的。

From the answers I've gotten so far, it looks like the only way to accomplish this is by storing the string in the exception. The Boost guidelines recommend against this, which is confusing to me, because std::runtime_error does just that.

即使我使用C字符串,也必须使用静态大小的缓冲区,或者执行也会失败的内存管理。 (我想知道这是否真的是 std :: string 的copy-constructor中唯一会出错的东西。这意味着我不会使用任何东西动态分配的C字符串。)

Even if I were to use a C-string, I'd have to use a statically sized buffer, or do memory management which can fail too. (I'm wondering if this is actually the only thing that can go wrong in std::string's copy-constructor. That would mean I won't gain anything using dynamically allocated C-strings.)

还有其他选择吗?

推荐答案

我的异常类通常除了构造函数外什么都没有,并且遵循以下原则:

My exception classes generally don't have anything but the constructor and look along these lines:

class MyEx: public std::runtime_error 
{
public: 
    MyEx(const std::string& msg, int line): 
        std::runtime_error(msg + " on line " + boost::lexical_cast<string>(line)) 
    {} 
}; 

一个任意示例,但这是处理内容的基类()消息。

An arbitrary example, but it is the base class that handles managing the what() message.

但是,如果您愿意,也可以在将消息放到构造函数主体中之后,才分配异常对象的基础部分。

But if you want to, you can also only assign the base part of the exception object, after you've put together the message in the constructor body.

#include <stdexcept>
#include <string>
#include <sstream>

class MyEx: public std::runtime_error
{
public:
    MyEx(const std::string& msg, int line):
        std::runtime_error("")
    {
        std::stringstream ss;
        ss << msg << " on line " << line;
        static_cast<std::runtime_error&>(*this) = std::runtime_error(ss.str());
    }
};

#include <iostream>
int main()
{
    try {
        throw MyEx("Evil code", __LINE__);
    }
    catch (const std::exception& e) {
        std::cout << e.what() << '\n';
    }
}






但是,关于增强的指导原则,也许您应该注意一点,即最好通过其他方法将数字数据(位置和线条)作为数字提供。准则要求减少对 what()消息的担心。

这篇关于从std :: exception的what返回一个动态字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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