#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
using namespace std;
int main()
{
vector<int> v = { 1, 2, 3, 4, 5, 6 };
for (int i = 0; i <= v.size(); i++)
{
cout << v[i] << " ";
//cout << v.at(i) << " ";
}
cout << endl;
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
using namespace std;
int main()
{
vector<int> v = { 1, 2, 3, 4, 5, 6 };
for (int i = 0; i <= v.size(); i++)
{
//cout << v[i] << " ";
cout << v.at(i) << " ";
}
cout << endl;
return 0;
}
实际中C语言基本都是使用返回错误码的方式处理错误,部分情况下使用终止程序处理非常严重的错误。
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
using namespace std;
int main()
{
try
{
vector<int> v = { 1, 2, 3, 4, 5, 6 };
for (int i = 0; i <= v.size(); i++)
{
//cout << v[i] << " ";
cout << v.at(i) << " ";
}
cout << endl;
}
catch (exception& e)
{
cout << e.what() << endl;
}
return 0;
}
如果有一个块抛出一个异常,捕获异常的方法会使用 try 和 catch 关键字。try 块中放置可能抛出异常的代码,try 块中的代码被称为保护代码。使用 try/catch 语句的语法如下所示:
try
{
// 保护的标识代码
}
catch (ExceptionName e1)
{
// catch 块
}
catch (ExceptionName e2)
{
// catch 块
}
catch (ExceptionName eN)
{
// catch 块
}
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
namespace yyw
{
int div(int n, int m)
{
if (m == 0)
{
//throw - 1;
//throw "发生除0错误!";
throw string("发生除0错误!");
}
return n / m;
}
}
int main()
{
try
{
int n = 0;
int m = 0;
cin >> n >> m;
cout << yyw::div(n, m) << endl;
}
catch (int err)
{
cout << err << endl;
}
catch (const char* err)
{
cout << err << endl;
}
catch (...)
{
cout << "unkown exception!" << endl;
}
}
有可能单个的catch不能完全处理一个异常,在进行一些校正处理以后,希望再交给更外层的调用链函数来处理,catch则可以通过重新抛出将异常传递给更上层的函数进行处理。
#define _CRT_SECURE_NO_WARNINGS 1
#include
using namespace std;
double Division(int a, int b)
{
// 当b == 0时抛出异常
if (b == 0)
{
throw "Division by zero condition!";
}
return (double)a / (double)b;
}
void Func()
{
// 这里可以看到如果发生除0错误抛出异常,另外下面的array没有得到释放。
// 所以这里捕获异常后并不处理异常,异常还是交给外面处理,这里捕获了再
// 重新抛出去。
int* array = new int[10];
try {
int len, time;
cin >> len >> time;
cout << Division(len, time) << endl;
}
catch (...)
{
cout << "delete []" << array << endl;
delete[] array;
throw;
}
// ...
cout << "delete []" << array << endl;
delete[] array;
}
int main()
{
try
{
Func();
}
catch (const char* errmsg)
{
cout << errmsg << endl;
}
return 0;
}
// 这里表示这个函数会抛出A/B/C/D中的某种类型的异常
void fun() throw(A,B,C,D);
// 这里表示这个函数只会抛出bad_alloc的异常
void* operator new (std::size_t size) throw (std::bad_alloc);
// 这里表示这个函数不会抛出异常
void* operator new (std::size_t size, void* ptr) throw();
实际使用中很多公司都会自定义自己的异常体系进行规范的异常管理,因为一个项目中如果大家随意抛异常,那么外层的调用者基本就没办法玩了,所以实际中都会定义一套继承的规范体系。这样大家抛出的都是继承的派生类对象,捕获一个基类就可以了。
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
using namespace std;
class Exception
{
public:
Exception(const char* errmsg, int errid)
:_errmsg(errmsg)
, _errid(errid)
{}
virtual string what() = 0;
protected:
string _errmsg; //错误描述
int _errid; //错误码
//stack _st //调用栈帧
};
class SqlException :public Exception
{
public:
SqlException(const char* errmsg, int errid)
:Exception(errmsg, errid)
{}
virtual string what()
{
return "数据库错误:" + _errmsg;
}
};
class NetWorkException :public Exception
{
public:
NetWorkException(const char* errmsg, int errid)
:Exception(errmsg, errid)
{}
virtual string what()
{
return "网络错误:" + _errmsg;
}
};
void ServerStart()
{
//模拟一下问题抛出异常报错
if (rand() % 8 == 0)
{
throw SqlException("数据库启动失败", 1);
}
if (rand() % 7 == 0)
{
throw NetWorkException("网络连接失败", 3);
}
cout << "正常运行" << endl;
}
int main()
{
for (size_t i = 0; i < 100; i++)
{
try
{
ServerStart();
}
catch (Exception& e)
{
cout << e.what() << endl;
}
catch (...)
{
cout << "未知异常" << endl;
}
}
return 0;
}
C++ 提供了一系列标准的异常,定义在 中,我们可以在程序中使用这些标准的异常。它们是以父子类层次结构组织起来的,如下所示:
总结:异常总体而言,利大于弊,所以工程中我们还是鼓励使用异常的。另外OO的语言基本都是用异常处理错误,这也可以看出这是大势所趋。
以上就是今天要讲的内容,本文介绍了C++异常概念、用法、以及优缺点,异常提供了大量能使我们快速便捷地了解错误的发生,我们务必掌握。另外如果上述有任何问题,请懂哥指教,不过没关系,主要是自己能坚持,更希望有一起学习的同学可以帮我指正,但是如果可以请温柔一点跟我讲,爱与和平是永远的主题,爱各位了。