Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ boost异常';s错误信息_C++_Exception_Boost - Fatal编程技术网

C++ boost异常';s错误信息

C++ boost异常';s错误信息,c++,exception,boost,C++,Exception,Boost,使用时,了解异常携带的数据的首选方法是什么?在以下示例中,给出了以下示例: catch( io_error & e ) { std::cerr << "I/O Error!\n"; if( std::string const * fn=get_error_info<file_name>(e) ) std::cerr << "File name: " << *fn << "\n";

使用时,了解异常携带的数据的首选方法是什么?在以下示例中,给出了以下示例:

catch( io_error & e )
    {
    std::cerr << "I/O Error!\n";

    if( std::string const * fn=get_error_info<file_name>(e) )
        std::cerr << "File name: " << *fn << "\n";

    if( int const * c=get_error_info<errno_code>(e) )
        std::cerr << "OS says: " << strerror(*c) << "\n";
    }
然后,我可以在catch块中更改此信息:

catch (FileException& e) {
  e.filename = filename;
  throw;
}

通过这种简单的方法,我可以绕过大部分文档,并从所有的安全性中获益(例如,不尝试从
MathException
获取文件名)。然而,人们使用boost。我缺少的动态方法有很大的优势吗?您如何在代码中记录错误信息?

如果您可以更改或设计异常以包含描述错误所需的内容,请随意操作。但是,如果异常不是由您的代码生成的,或者您在生成错误时无法访问错误信息,那么您需要使用
error\u info
的动态方法。例如,我们认为有一个<代码> Read < /Cord> Free函数,它从C++文件对象读取数据,其中不包含文件名,如下:

void read( file& fp, std::vector<unsigned char>& buf ) {
    if( fread(&buf[0], 1, buf.size(), fp.getFile()) == -1 ) {
        // I don't know the file name, what should I do here??
        boost::throw_exception( io_error() );
    }
}

try {
    file fp( "/path/to/file" );
    // do some thing with fp
    read( fp, buffer );
} catch( io_error& e ) {
    e << file_name( "/path/to/file" ); // here I can provide file name
    throw;
}
void读取(文件&fp,标准::向量&buf){
if(fread(&buf[0],1,buf.size(),fp.getFile())=-1){
//我不知道文件名,我该怎么办??
boost::抛出异常(io_error());
}
}
试一试{
文件fp(“/path/to/file”);
//用fp做点什么
读取(fp,缓冲区);
}捕获(io_错误和e){

e从一个非常高的级别,您可以参考该异常的诊断信息:

这将打印异常携带的所有内容(以及更多内容)。我不确定是否有办法单独获取所有内部内容

从示例中可以看出:

example_io.cpp(70): Throw in function class boost::shared_ptr<struct _iobuf> __cdecl my_fopen(const char *,const char *)
Dynamic exception type: class boost::exception_detail::clone_impl<struct fopen_error>
std::exception::what: example_io error
[struct boost::errinfo_api_function_ *] = fopen
[struct boost::errinfo_errno_ *] = 2, "No such file or directory"
[struct boost::errinfo_file_name_ *] = tmp1.txt
[struct boost::errinfo_file_open_mode_ *] = rb
example\u io.cpp(70):插入函数类boost::shared\u ptr\u cdecl my\u fopen(const char*,const char*)
动态异常类型:类boost::异常\u详细信息::克隆\u impl
std::exception::what:示例io错误
[struct boost::errinfo\u api\u function\u*]=fopen
[struct boost::errinfo_errno_*]=2,“没有这样的文件或目录”
[struct boost::errinfo_file_name_*]=tmp1.txt
[struct boost::errinfo_file_open_mode_*]=rb

动态方法的优势文件名不仅仅与FileException对象相关。例如,如果打开一个脚本文件,其中包含一个数学表达式,在解析时抛出一个MathException,您仍然希望该异常包含该文件名。通常,抛出站点和捕获站点之间的调用堆栈无法知道所有可能的异常类型对象可能会通过它,但它通常具有应该存储在其中的相关信息。

我了解该应用程序,但为什么您不能在
catch
块中填写我的问题中的示例
结构
,然后重新显示它?有时您正在处理一个应用程序,并且使用所有库来完成在你的任务中,现在你完全可以自由地使用任何类型的工具来完成你的任务,例如,你可以抛出一个非
std::exception
,所有的事情都会很好地工作。但是有时候你正在处理一个介于其他两部分代码之间的库,那么你就不能更改任何东西,而且它还有一点性能优势使用
error\u info
创建一个新的结构并重新显示它,但只假设其他两个库也使用boost异常,对吗?在一个新项目中使用它比使用我自己的异常类有好处吗?我将如何记录错误信息的使用?不,动态
error\u info
非常好,因为库可以使用
std::exception
并且你可以在你的程序中使用
boost::error\u info
!这太棒了,不是吗?对不起,我不明白。我不能将
error\u info
附加到非boost异常上,可以吗?所以如果库抛出一个基于
std::exception
的东西,我必须以
boost::exception
的形式重新调用它嗯?
example_io.cpp(70): Throw in function class boost::shared_ptr<struct _iobuf> __cdecl my_fopen(const char *,const char *)
Dynamic exception type: class boost::exception_detail::clone_impl<struct fopen_error>
std::exception::what: example_io error
[struct boost::errinfo_api_function_ *] = fopen
[struct boost::errinfo_errno_ *] = 2, "No such file or directory"
[struct boost::errinfo_file_name_ *] = tmp1.txt
[struct boost::errinfo_file_open_mode_ *] = rb