异常
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
char GetChars(string &str, const int nStrIndex)
{
if (nStrIndex > (int)str.size())
{
throw string("索引超过字符长度");
}
return str[nStrIndex];
}
int main()
{
string str("Hello World");
char ch;
try
{
ch = GetChars(str, 100);//如果这个地方抛出了异常,那么 cout << ch << endl; 是不会执行的,执行流已经转移到catch了
cout << ch << endl;
}
catch (string &aval)
{
cout << aval << endl;
}
catch (...)
{
cout << "这是默认的处理异常,类似default" << endl;
}
system("pause");
return 0;
}