std::nested_exception 和多态性 - 这是可以做到的最好的吗?

问题描述

当您只想调用 std::nested_exceptions 时,

what() 很好,但是访问其他异常类型的接口变得很丑陋。

假设我有两个异常类,它们存储了一些附加信息:

/* CODE BLOCK 1 */
class ErrorI : public std::runtime_error {
public:
    ErrorI(int a_integer) : std::runtime_error{"ErrorI"},integer{a_integer} {}
    int integer;
};

class ErrorD : public std::runtime_error {
public:
    ErrorD(double a_real) : std::runtime_error{"ErrorD"},real{a_real} {}
    double real;
};

没有嵌套异常,我们可以访问try/catch块中的成员变量:

/* CODE BLOCK 2 */
int main()
{
    try {
        /* do stuff */;
    }
    catch(const ErrorI& ee){
        std::cout << "  Value: " << ee.integer << std::endl;
    }
    catch(const ErrorD& ee){
        std::cout << "  Value: " << ee.real << std::endl;
    }
}

但是如果我们想解开一个 std::nested_exception,事情就没有那么简单了。我们需要定义一个将被递归调用函数,它应该是这样的:

/* CODE BLOCK 3 */
void process_exception(const std::exception& e,int level=0) {
    try {
        std::rethrow_if_nested(e);
    }
    catch(const std::exception& e) {
        process_exception(e,level+1);
    }

    /* ... process the top-most (latest) exception ... */
}

不幸的是,为了处理最顶层的异常,我们不能在代码块 2 中使用 try/catch 语法:如果我们重新抛出 e,它将被截断为 std::exception,我们将失去所有附加信息。 编辑:如果使用 std::rethrow_exception 和 std::exception_ptr,这不是真的。

所以我们又回到了 good-ole 动态类型检查的问题,以及它所需要的一切(例如,参见 this)。

  1. 从具有所需接口的公共基类派生所有异常。这包括 Visitor pattern 之类的方法。这很好,但如果异常类是由外部库提供的,那就不好了。

  2. 使用 dynamic_cast:

     /* CODE BLOCK 4 */
     if (auto p = dynamic_cast<ErrorI const*>(&e)) {
         std::cout << "  Value: " << p->integer << std::endl;
     }
     else if (auto p = dynamic_cast<ErrorD const*>(&e)) {
         std::cout << "  Value: " << p->real << std::endl;
     }
    
  3. ???

我唯一的选择似乎是求助于 2。如果有任何其他建议,我很想听听。

解决方法

std::exception_ptr 的帮助下,您可以执行以下操作:

void print_exception(const std::exception_ptr& eptr,int level =  0)
{
    try
    {
        std::rethrow_exception(eptr);
    }
    catch (const ErrorI& e)
    {
        std::cerr << std::string(level,' ') << "exception: " << e.what() << ": " << e.integer << std::endl;
    }
    catch (const ErrorD& e)
    {
        std::cerr << std::string(level,' ') << "exception: " << e.what() << ": " << e.real << std::endl;
    }
    catch (const std::exception& e)
    {
        std::cerr << std::string(level,' ') << "exception: " << e.what() << std::endl;
    }
}

// prints the explanatory string of an exception. If the exception is nested,// recurses to print the explanatory of the exception it holds
void print_exception_rec(const std::exception_ptr& eptr,int level =  0)
{
    print_exception(eptr,level);
    try {
        try {
            std::rethrow_exception(eptr);
        }
        catch (const std::exception& e)
        {
            std::rethrow_if_nested(e);
        }
    } catch(const std::exception&) {
        print_exception_rec(std::current_exception(),level+1);
    } catch(...) {}
}

Demo

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...