【发布时间】:2010-04-27 04:40:25
【问题描述】:
我只是在学习如何处理我的 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;
}
现在我有两个问题。首先,我想知道我是否正确使用了异常。其次,(假设第一个是真的)使用它们与 If else 语句相比有什么好处?
【问题讨论】: