How to simulate inner exception in C++

前端 未结 6 1584
逝去的感伤
逝去的感伤 2021-01-02 19:06

Basically I want to simulate .NET Exception.InnerException in C++. I want to catch exception from bottom layer and wrap it with another exception and throw again to upper la

6条回答
  •  鱼传尺愫
    2021-01-02 19:12

    Since C++ 11 you have new options:

    1. You can use std::exception_ptr.

      The exception is then preserve until last exception_ptr to this exception is destroyed.

      struct base_exception : public std::exception
      {
          std::exception_ptr InnerException;
      
          base_exception() {}
          base_exception(std::exception& innerException)
           : InnerException(std::make_exception_ptr(innerException))
          {}
      
      };
      
    2. Or you can simply use std::nested_exception.

提交回复
热议问题