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

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

问题描述

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

  bool parseFile(ManagedClass *%parsedFileAsManagedObject,const string& fileName); 
std :: string line;
bool success = true;
ifstream is(fileName.c_str());
//除非我注释掉下面这行我得到的问题
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;
//点击调试器。
}
return success;
}

当运行如上时,我得到:


$ _CxxThrowException(Void *,_s__ThrowInfo *)。


外部组件已抛出异常



<



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



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



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



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> > .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



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



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> / code>在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和settings\KingKewlio\my documents \visual studio 2008 \projects\myproj\myprojrepeated\levelparser.cpp:第355行


描述我的例外。



当我注释掉这一行,所有都在Interop的土地上。



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



我认为Interop是因为当我不打扰环绕 getline(...)在中尝试 catch 错误


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) 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 ++


回答 http://stackoverflow.com/q/8144268/866333 到我上一个问题表示这是意外的行为,但我

code> std :: ios_base c $ c>抛出异常。因为它是C ++标准库的一部分,所以异常可能是 std :: exception& 。不是异常^ 。因为你的代码不捕获 std :: exception& ,它可能要终止。这可以通过添加

  catch(...){
log(Unknown exception thrown! );
throw; // rethrow it。总是返回未知的例外。期。
}

此外, getline code>将同时设置 EOF failbit 该文件在空行结束。


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;
}

When run as above I get:

External component has thrown an exception.

at _CxxThrowException(Void* , _s__ThrowInfo* )

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,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) in c:\program files\microsoft visual studio 9.0\vc\include\ios:line 55

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

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) 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) 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) in c:\documents and settings\KingKewlio\my documents\visual studio 2008\projects\myproj\myprojrepeated\levelparser.cpp:line 355

describing my exception.

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

Can anybody explain why Interop doesn't like it?

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

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) Line 294 + 0x3a bytes C++

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

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

myproj.exe!std::basic_istream<char,std::char_traits<char> >::_Ipfx(bool _Noskip = true) Line 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) 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 ' ') Line 483 + 0xe 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 = {...}) Line 531 + 0x33 bytes C++

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

解决方案

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.
}

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

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

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