簡體   English   中英

從boost :: exception和std :: runtime_error繼承自定義異常類

[英]Inherit custom exception class from both boost::exception and std::runtime_error

我想介紹我的自定義異常類的層次結構,派生自boost :: exception和std :: runtime_error,以便what()返回有意義的東西。

到目前為止,我沒有運氣:

#include <iostream>
#include <stdexcept>

#include <boost/exception/all.hpp>

typedef boost::error_info<struct tag_foo_info, unsigned long> foo_info;

struct foo_error : virtual boost::exception, virtual std::runtime_error
{
  explicit foo_error(const char *const what)
    : std::runtime_error(what)
  { }
};

static void foo()
{
  BOOST_THROW_EXCEPTION(foo_error("foo error") << foo_info(100500));
}

int main(int argc, char *argv[])
{
  try
  {
    foo();
  }
  catch (const std::exception& e)
  {
    std::cerr << boost::diagnostic_information(e);
    return 1;
  }

  return 0;
}

只是一直抱怨std::runtime_error no appropriate default constructor available

我能得到的最接近的是使用實際的std::runtime_error

BOOST_THROW_EXCEPTION(boost::enable_error_info(std::runtime_error("foo error")) << foo_info(100500)))

但這不是我想要的。 基本上,我想一個異常類是catch能夠通過catch (const std::exception& e) catch (const std::runtime_error& e) catch (const boost::exception& e)catch (const foo_error& e) 那可能嗎? 先感謝您。

你需要公共繼承

struct Exception : public boost::exception, public std::runtime_error
{
    Exception()
    :   std::runtime_error("Hello World")
    {}
};

int main()
{
    try {
        try {
            throw Exception();
        }
        catch(const std::runtime_error&) {
            std::cout << "std::runtime_error" << std::endl;
            throw;
        }
    }
    catch(const boost::exception&) {
        std::cout << "boost::exceptionr" << std::endl;
    }
    return 0;
}

如果替換兩個虛擬代碼,您的代碼將起作用:

Throw in function void foo()
Dynamic exception type: boost::exception_detail::clone_impl<foo_error>
std::exception::what: foo error
[tag_foo_info*] = 100500

boost異常庫有一個派生自異常的類:

// Curiously recurring template pattern (exception.hpp:419:20)
class clone_impl: public Exception, public clone_base;

由於虛擬繼承,大多數派生類負責初始化基類(clone_impl不支持)

std :: runtime_error已經從std :: exception繼承。 所以你只需要繼承std :: runtime_error就可以了。

更新:我的意思是繼承std :: runtime_error。 如果您嘗試這樣做怎么辦:

#include <iostream>
#include <stdexcept>

struct foo_error : public std::runtime_error
{
    explicit foo_error(const char *const what)
        : std::runtime_error(what)
    { }
};

static void foo()
{
    throw foo_error("foo error");
}

int main(int argc, char *argv[])
{
    try
    {
        foo();
    }
    catch (const std::exception& e)
    {
        std::cerr << boost::diagnostic_information(e);
        return 1;
    }
    return 0;
}

為簡單起見省略了提升的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM