我只是学习如何处理我的C++代码中的错误.我写了这个示例,查找一个名为some file的文本文件,如果找不到它将抛出异常.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int array[90];
try
{
ifstream file;
file.open("somefile.txt");
if(!file.good())
throw 56;
}
catch(int e)
{
cout<<"Error number "<<e<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在我有两个问题.首先,我想知道我是否正确使用了例外.第二,(假设第一个是真的)使用它们对If else语句有什么好处?