Trty Catch

异常处理的一些操作和步骤

  1. 一个try可以对应对个catch
1
2
3
4
5
6
7
8
9
10
11
f(int a){
if( a < 1 )
throw a;
}
try{

}
catch(int)
{
cout << "a小于1";
}

可以单独写一个类处理和接受异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;
//异常类
class listException{
pubilc:
listException(string str):str(str){}
void print()
{
cout << str <<endl;
}
protected:
string str;
};
void deleteList()
{
if(size == 0)
throw listException("链表为空");
cout << "链表正常删除!";
}
int main ()
{
try
{
deleteList();
}

catch(listException object)
{
object.print();
}

system("pause");
return 0;
}

C++有专门的一个类用以异常处理

1
#include <exception> 
  1. bad_alloc

    内存处理:防止溢出与内存超出

  2. bad_cast

  3. bad_typeid