私は最近boost :: exceptionの使用を開始しました。今私は、boost :: errinfo_nested_exceptionを使ってエラーの原因に関する情報を出力したいと思います。問題は、原因から情報を取得する方法を理解できないことです。ノー成功を収めて、次の試してみました:boost :: errinfo_nested_exceptionから情報を抽出するにはどうすればよいですか?
#include <iostream>
#include <boost/exception/all.hpp>
struct myex : public virtual boost::exception {};
int main()
{
myex cause;
cause << boost::errinfo_file_name("causefile.cpp");
try {
myex ex;
ex << boost::errinfo_nested_exception(boost::copy_exception(cause));
throw ex;
}
catch (myex& e) {
// Here I would like to extract file name from cause and print
// it in a nice way, but I cant figure out what to do with a
// boost::exception_ptr.
const boost::exception_ptr* c =
boost::get_error_info<boost::errinfo_nested_exception>(e);
// I cant do this:
// const std::string* file = boost::get_error_info<boost::errinfo_file_name>(*c);
// Nor this:
// const std::string* file = boost::get_error_info<boost::errinfo_file_name>(**c);
// This works fine and the nested exception is there, but that's not what I want.
std::cout << boost::diagnostic_information(e) << std::endl;
}
return 0;
}
ビットそれをretrhowする必要があるために、奇妙な、しかし私は、ブーストで奇妙だと思います。多分それはタイピングと関係があります。とにかく、それは私の問題を解決しました。 ;) – ygram
@ygramこれは 'boost :: exception_ptr'がどのように指定されているかということから来ています。そのインタフェースは非常に最小限であり、(ポインタ自体ではなく)* stored *例外に役立つ何かを行う唯一の方法は、 。それが 'std :: exception_ptr'に受け入れられたことと、言語が例えば' 'int'は、実際にはかなり固いデザインです。 –