调用IOS :: exception(...)导致EOF在C ++ - CLI中的std :: getline(...)中抛出InteropServices.SEHException,为什么? [英] Calling IOS::exception(...) causes EOF to throw InteropServices.SEHException from std::getline(...) in C++-CLI, why?

查看:207
本文介绍了调用IOS :: exception(...)导致EOF在C ++ - CLI中的std :: getline(...)中抛出InteropServices.SEHException,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I run the following function to load a file. It is being ported from CLI to standard C++ and exhibits unexpected behaviour when I attempt to specify that I would like to throw all exceptions except 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;
}

当运行如上,我得到:


外部组件引发了异常。

External component has thrown an exception.

at _CxxThrowException(Void *,_s__ThrowInfo *)

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

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

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

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

at 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行

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

at std.basic_istream< char,std :: char_traits< char>在C:\program files\microsoft visual studio 9.0 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ include \istream:第120行

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

std.basic_istream< char,std :: char_traits< char>                  < char& visual studio 9.0\vc\include\istream:第76行

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

at 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

at 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) in c:\program files\microsoft visual studio 9.0\vc\include\string:line 483

at 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) / code> c:\program files\microsoft visual studio 9.0\vc\include\string:line 531

at 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) in c:\program files\microsoft visual studio 9.0\vc\include\string:line 531

at 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

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

描述我的例外。

当我评论这一行都在Interop的地方很好。

When I comment out that line all is well in the land of Interop.

任何人都可以解释为什么Interop不喜欢?

Can anybody explain why Interop doesn't like it?

我认为Interop是因为当我不打扰环绕那个 getline(...) in 尝试 catch 我收到以下错误

I attribute Interop because when I don't bother to surround that getline(...) in try and catch I get the following error


System.Runtime.InteropServices.SEHException

System.Runtime.InteropServices.SEHException

从上面显示的调用堆栈顶部报告,再次显示如下:

reported from the top of the call stack shown above and again below:


[管理到本机转换]

[Managed to Native Transition]

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

myproj.exe!std::ios_base::clear(int _State = 3, bool _Reraise = false) Line 294 + 0x3a bytes C++

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

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

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

myproj.exe !性病:: basic_istream<炭,性病:: char_traits<炭> > :: _ Ipfx(bool _Noskip = true)第121行C ++

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

myproj.exe!std :: basic_istream< ;炭,性病:: char_traits<炭> > :: sentry :: sentry(std :: basic_istream< char,std :: char_traits< char>& _Istr = {...},bool _Noskip = true) 76行+ 0x18 bytes 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) Line 76 + 0x18 bytes 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 = {...}, char _Delim = 10 ' ') Line 483 + 0xe bytes C++

<$ !C $ C> myproj.exe的std ::函数getline<炭,性病:: char_traits<炭>,的std ::分配器<炭> >(std :: basic_istream< char,std :: char_traits< char>& _Istr = {...},std :: basic_string< char,std :: char_traits< char>,std :: allocator< char> ;& _Str = {...})行531 + 0x33字节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 = {...}) Line 531 + 0x33 bytes C++

回答我上一个问题的 https://stackoverflow.com/q/8144268/866333 表示这是意想不到的行为,但我我准备被证明是错误的。

An answer https://stackoverflow.com/q/8144268/866333 to my previous question indicates that this is unexpected behaviour but I am prepared to be proved wrong.

推荐答案

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

It would appear as if std::ios_base is throwing an exception. Since it is part of the C++ standard library, the exception is probably a std::exception &. Not a Exception ^. Since your code doesn't catch std::exception &, it is probably about to terminate. This can be confirmed by adding a

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

另外, getline(is,line) code>将同时设置 EOF failbit 文件以空行结尾。

Also, getline(is, line) will set both EOF and failbit both, if the file ends in an empty line.

这篇关于调用IOS :: exception(...)导致EOF在C ++ - CLI中的std :: getline(...)中抛出InteropServices.SEHException,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆