文件打开/关闭状态引发意外异常

unexpected exception thrown for file open/close status

本文关键字:意外 异常 状态 文件      更新时间:2023-10-16

嗨,我有代码的样本

try {
ifs.open(sourceHtmlFile.c_str());
std::cout << ifs << "n" ;
if( !ifs ) ;
throw std::string("Could not open file");
} catch (std::string &ex) {
std::cout << ex << " !n";
}

o/p

当文件不存在时:如果->0引发预期的异常

当文件确实存在时如果->0xbfc885b4但它还是抛出了异常。

我想知道为什么它在两种情况下都抛出异常

谢谢,

if条件之后有一个额外的;。这个分号终止了条件语句,所以throw总是被执行。