我有以下的try/catch:C++的try/catch不能捕獲的異常
try
{
Player newPlayer = Database::newPlayer(atoi(arguments.at(1).c_str()), arguments.at(2));
}
catch (int e)
{
cout << "Wrong parameters used.\n";
}
我的目標是能夠增加一名球員,如果由於某種原因,參數不匹配,如把一個非整數arguments.at(1)
或沒有足夠的參數,但在Visual Studio中,如果我這樣做,那麼程序崩潰,並且Visual Studio說「未處理的異常,在內存位置std :: out_of_range」。我希望我的程序不會在發生這種情況時崩潰,而只是簡單地說出「錯誤的參數使用」,因爲它目前在catch語句中。
您正在捕獲一個'int'。你知道這個對嗎? – Rapptz
要捕捉所有內容,請使用'catch(...)',但不要這樣做。 – doug65536
你需要趕上std :: out_of_range而不是int – Pedrom