![在这里插入图片描述](https://img-blog.csdnimg.cn/20210306165029216.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUzMTU3MTcz,size_16,color_FFFFFF,t_70)
1. 如果try语句里面的语句都没有出现异常,就会执行catch后面的代码块
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210306170601321.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUzMTU3MTcz,size_16,color_FFFFFF,t_70)
2.try语句里面存在语句抛出异常,会去下面的catch块中寻找抛出异常类型相同的语句块
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210306171058125.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUzMTU3MTcz,size_16,color_FFFFFF,t_70)
3. try语句抛出异常,但是下面的catch语句块中没有一个能够捕获该异常,那么会跳转到catch下面的语句,造成程序的终止,因为异常没有被解决
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210306171407453.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUzMTU3MTcz,size_16,color_FFFFFF,t_70)
会丢出异常的情况
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210306171603433.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUzMTU3MTcz,size_16,color_FFFFFF,t_70)
自定义异常类
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210306171655680.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUzMTU3MTcz,size_16,color_FFFFFF,t_70)
异常捕获优化c++写的顺序栈
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
const int MAX = 1024;
class stack
{
private:
void** data;
int size;
int top;
public:
stack();
stack(int size);
~stack();
void push(void* val);
void pop();
void* getTop();
bool isEmpty();
bool isFull();
void setNull();
class Empty{
};
class Full{
};
};
stack::stack()
{
cout << "无参构造函数调用" << endl;
size = MAX;
top = -1;
data = new void*[MAX];
}
stack::stack(int size)
{
cout << "有参构造函数调用" << endl;
this->size = size;
top = -1;
data = new void*[size];
}
stack::~stack()
{
cout << "析构函数" << endl;;
delete[] data;
}
void stack::push(void* val)
{
if (val == NULL)
return;
if (isFull())
{
throw Full();
}
else
{
data[++top] = val;
}
}
void stack::pop()
{
if (isEmpty())
{
throw Empty();
}
else
{
--top;
}
}
void* stack::getTop()
{
if (!isEmpty())
{
return data[top];
}
return NULL;
}
bool stack::isFull()
{
if (top+1==size)
return true;
return false;
}
bool stack::isEmpty()
{
if (top == -1)
return true;
return false;
}
void stack::setNull()
{
top = -1;
}
class person {
public:
string name;
int age;
};
void test()
{
person p[3] = {
{
"大忽悠",19},{
"小朋友",19},{
"大猪头",18} };
stack s1(3);
try {
s1.push(&p[0]);
s1.push(&p[1]);
s1.push(&p[2]);
s1.push(&p[2]);
}
catch (stack::Full)
{
cout << "FULL!!" << endl;
}
try
{
person* p1 = (person*)s1.getTop();
cout << "姓名: " << p1->name << " 年龄:" << p1->age << endl;
s1.pop();
person* p2 = (person*)s1.getTop();
cout << "姓名: " << p2->name << " 年龄:" << p2->age << endl;
s1.pop();
person* p3 = (person*)s1.getTop();
cout << "姓名: " << p3->name << " 年龄:" << p3->age << endl;
s1.pop();
person* p4 = (person*)s1.getTop();
cout << "姓名: " << p4->name << " 年龄:" << p4->age << endl;
s1.pop();
}
catch (stack::Empty)
{
cout << "Empty!!" << endl;
}
}
int main()
{
test();
system("pause");
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210306173842295.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUzMTU3MTcz,size_16,color_FFFFFF,t_70)
vs2019编译器会出错
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021030617405064.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUzMTU3MTcz,size_16,color_FFFFFF,t_70)