2017-02-23 92 views
-1

这里的初学程序员在Visual Studio中工作。throw导​​致代码崩溃而不是仅仅退出

vector<string> fileParse(vector<string> & inputStrings, string & fileName) { 
    ifstream x; 
    x.open(fileName); 
    cout << "attempting to open file" << endl; 
    if (!x.is_open()) { 
     cout << "Bad input file name. Input was: " << fileName << endl; 
     throw lab0badInput; 
    } 
    else { 
     string temp; 
     while (x >> temp) { 
      inputStrings.push_back(temp); 
     } 
     x.close(); 
    } 
    return inputStrings; 
} 

调用throw导致我的程序崩溃,而不是扔正确的值和退出的。有人可以解释为什么吗?

谢谢!

+0

你得到一个错误信息,当崩溃发生? – Amber

+0

应该在哪里抛出正确的值? PLZ在你的程序中捕获你的异常并调用std :: exit() –

+2

Crash ==程序退出。 –

回答

-1

使用尝试和catch块来代替:

try { /* */ } catch (const std::exception& e) { /* */ }