【问题标题】:Calling IOS::exception(...) causes EOF to throw InteropServices.SEHException from std::getline(...) in C++-CLI, why?调用 IOS::exception(...) 会导致 EOF 在 C++-CLI 中从 std::getline(...) 抛出 InteropServices.SEHException,为什么?
【发布时间】:2011-11-22 12:58:39
【问题描述】:

我运行以下函数来加载文件。它正在从 CLI 移植到标准 C++ 并在我尝试指定要抛出除 EOF 之外的所有异常时表现出意外行为。

bool parseFile(ManagedClass *%parsedFileAsManagedObject, const string &fileName);
    std::string line;
    bool success = true;
    ifstream is(fileName.c_str());
    //Unless I comment out the following line I get problems
    is.exceptions( ifstream::badbit | ifstream::failbit );
    //do stuff
    try {
        while (getline(is, line)) {
        //do stuff again
            parsedFileAsManagedObject = gcnew ManagedClass(12, 44);
        }
    } catch (Exception ^ex) {
        log(ex->ToString() + ex->StackTrace + ex->InnerException);
        success = false;
        //Hit debugger here.
    }
    return success;
}

如上运行时,我得到:

外部组件抛出异常。

_CxxThrowException(Void* , _s__ThrowInfo* )

std.ios_base.clear(ios_base* , Int32 _State, Boolean _Reraise) 在 c:\program files\microsoft visual studio 9.0\vc\include\xiosbase:line 294 中

std.basic_ios<char,std::char_traits<char> >.clear(basic_ios<char\,std::char_traits<char> >* , Int32 _State, Boolean _Reraise) 在 c:\program files\microsoft visual studio 9.0\vc\include\ios:第 44 行

std.basic_ios<char,std::char_traits<char> >.setstate(basic_ios<char\,std::char_traits<char> >* , Int32 _State, Boolean _Reraise) 在 c:\program files\microsoft visual studio 9.0\vc\include\ios:第 55 行

std.basic_istream<char,std::char_traits<char> >._Ipfx(basic_istream<char\,std::char_traits<char> >* , Boolean _Noskip) 在 c:\program files\microsoft visual studio 9.0\vc\include\istream:line 120 中

std.basic_istream<char,std::char_traits<char> >.sentry.{ctor}(sentry* , basic_istream<char\,std::char_traits<char> >* _Istr, Boolean _Noskip) 在 c:\program files\microsoft visual studio 9.0\vc\include\istream:line 76 中

std.getline<char,struct std::char_traits<char>,class std::allocator<char> >(basic_istream<char\,std::char_traits<char> >* _Istr, basic_string<char\,std::char_traits<char>\,std::allocator<char> >* _Str, SByte _Delim) 在 c:\program files\microsoft visual studio 9.0\vc\include\string:line 483 中

std.getline<char,struct std::char_traits<char>,class std::allocator<char> >(basic_istream<char\,std::char_traits<char> >* _Istr, basic_string<char\,std::char_traits<char>\,std::allocator<char> >* _Str) 在 c:\program files\microsoft visual studio 9.0\vc\include\string:line 531 中

myprojrepeated.LevelParser.parseFile(ManagedClass *%parsedFileAsManagedObject, basic_string<char\,std::char_traits<char>\,std::allocator<char> >* fileName) 在 c:\documents and settings\KingKewlio\my documents\visual studio 2008\projects\myproj\myprojrepeated\levelparser.cpp:line 355 中

描述我的异常。

当我注释掉这行代码时,Interop 领域一切正常。

谁能解释为什么 Interop 不喜欢它?

我归因于 Interop,因为当我不费心将 getline(...) 包围在 trycatch 中时,我收到以下错误

System.Runtime.InteropServices.SEHException

从上面和下面显示的调用堆栈顶部报告:

[托管到原生转换]

myproj.exe!std::ios_base::clear(int _State = 3, bool _Reraise = false) 第 294 行 + 0x3a 字节 C++

myproj.exe!std::basic_ios<char,std::char_traits<char> >::clear(int _State = 3, bool _Reraise = false) 第 45 行 C++

myproj.exe!std::basic_ios<char,std::char_traits<char> >::setstate(int _State = 2, bool _Reraise = false) 第 56 行 C++

myproj.exe!std::basic_istream<char,std::char_traits<char> >::_Ipfx(bool _Noskip = true) 第 121 行 C++

myproj.exe!std::basic_istream<char,std::char_traits<char> >::sentry::sentry(std::basic_istream<char,std::char_traits<char> >& _Istr = {...}, bool _Noskip = true) 第 76 行 + 0x18 字节 C++

myproj.exe!std::getline<char,std::char_traits<char>,std::allocator<char> >(std::basic_istream<char,std::char_traits<char> >& _Istr = {...}, std::basic_string<char,std::char_traits<char>,std::allocator<char> >& _Str = {...}, char _Delim = 10 ' ') 第 483 行 + 0xe 字节 C++

myproj.exe!std::getline<char,std::char_traits<char>,std::allocator<char> >(std::basic_istream<char,std::char_traits<char> >& _Istr = {...}, std::basic_string<char,std::char_traits<char>,std::allocator<char> >& _Str = {...}) 第 531 行 + 0x33 字节 C++

对我上一个问题的回答 https://stackoverflow.com/q/8144268/866333 表明这是出乎意料的行为,但我准备好被证明是错误的。

【问题讨论】:

    标签: c++ exception c++-cli iostream


    【解决方案1】:

    看起来好像std::ios_base 正在引发异常。由于它是 C++ 标准库的一部分,因此异常可能是 std::exception &。不是Exception ^。由于您的代码没有捕捉到std::exception &,它可能即将终止。这可以通过添加

    来确认
    catch (...) {
        log("Unknown exception thrown!");
        throw;  //rethrow it.  ALWAYS RETHROW UNKNOWN EXCEPTIONS.  PERIOD.
    }
    

    另外,getline(is, line) 将同时设置EOF failbit 两者,如果文件以空行结束。

    【讨论】:

      猜你喜欢
      • 2012-08-02
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 2017-08-29
      相关资源
      最近更新 更多