一個文件流的析構函數可以拋出異常,例如,如果文件關閉操作失敗?文件流析構函數可以在C++中拋出異常嗎?
auto f = new std::ofstream("data.txt");
f->exceptions(std::ofstream::failbit | std::ofstream::badbit);
...
delete f; // May throw?
我可以通過手動關閉流來防止這種異常嗎?
auto f = new std::ofstream("data.txt");
f->exceptions(std::ofstream::failbit | std::ofstream::badbit);
...
f->close();
delete f; // May throw?
見https://stackoverflow.com/q/748014/72178。 – ks1322
沒有標準類從析構函數中拋出,也不應該拋出它。 – molbdnilo