#include <iostream>
using namespace std;
//传统的错误处理机制
//throw int类型异常
void my_strcpy1(char* to, char* from)
{
if (from == NULL)
{
throw 1;
}
if (to == NULL)
{
throw 2;
}
//copy是的 场景检查
if (*from == 'a')
{
throw 3; //copy时出错
}
while (*from != '\0')
{
*to = *from;
to++;
from++;
}
*to = '\0';
}
//传统的错误处理机制
//throw char*类型异常
void my_strcpy2(char* to, char* from)
{
if (from == NULL)
{
throw "源buf出错";
}
if (to == NULL)
{
throw "目的buf出错";
}
//copy是的 场景检查
if (*from == 'a')
{
throw "copy过程出错"; //copy时出错
}
while (*from != '\0')
{
*to = *from;
to++;
from++;
}
*to = '\0';
}
class BadSrcType {};
class BadDestType {};
class BadProcessType
{
public:
BadProcessType()
{
cout << "BadProcessType构造函数do \n";
}
BadProcessType(const BadProcessType& obj)
{
cout << "BadProcessType copy构造函数do \n";
}
~BadProcessType()
{
cout << "BadProcessType析构函数do \n";
}
};
//传统的错误处理机制
//throw 类对象 类型异常
void my_strcpy3(char* to, char* from)
{
if (from == NULL)
{
throw BadSrcType();
}
if (to == NULL)
{
throw BadDestType();
}
//copy是的 场景检查
if (*from == 'a')
{
printf("开始 BadProcessType类型异常 \n");
throw BadProcessType(); //会不会产生一个匿名对象?
}
if (*from == 'c')
{
throw new BadProcessType; //会产生一个匿名对象,抛出的是指针类型的错误
}
while (*from != '\0')
{
*to = *from;
to++;
from++;
}
*to = '\0';
}
void main()
{
int ret = 0;
char buf1[] = "acdefg";
char buf2[1024] = { 0 };
try
{
//my_strcpy1(buf2, buf1);
//my_strcpy2(buf2, buf1);
my_strcpy3(buf2, buf1);
}
catch (int e) //e可以写 也可以不写
{
cout << e << " int类型异常" << endl;
}
catch (char* e)
{
cout << e << " char* 类型异常" << endl;
}
//---
catch (BadSrcType e)
{
cout << " BadSrcType 类型异常" << endl;
}
catch (BadDestType e)
{
cout << " BadDestType 类型异常" << endl;
}
//结论1: 异常变量是对象的时候,使用对象变量来接,那么是把throw中新建的那个匿名对象copy给catch中的e,
//这其中会调用拷贝构造函数,相当于有了两个对象,最后调用两次析构函数
catch( BadProcessType e) //是把匿名对象copy给e
{
cout << " BadProcessType 类型异常" << endl;
}
//结论2: 使用引用的话 会使用throw时候的那个对象,catch中的e那个对象就是throw中新建的那个匿名对象
//前后只有一个对象,只会调用一次析构函数
//catch( BadProcessType &e) //e还是那个匿名对象
//{
// cout << " BadProcessType 引用类型异常" << endl;
//}
//结论3: 使用指针的话 会使用throw时候的那个对象,catch中的e那个对象就是throw中新建的那个匿名对象
//前后只有一个对象,只会调用一次析构函数
catch (BadProcessType * e) //e还是那个匿名对象
{
cout << " BadProcessType 指针类型异常" << endl;
delete e;
}
//结论4: 类对象时, 使用引用比较合适
// --
catch (...)
{
cout << "未知 类型异常" << endl;
}
cout << "hello..." << endl;
system("pause");
return;
}
//传统的错误处理机制
int my_strcpy(char* to, char* from)
{
if (from == NULL)
{
return 1;
}
if (to == NULL)
{
return 2;
}
//copy是的 场景检查
if (*from == 'a')
{
return 3; //copy时出错
}
while (*from != '\0')
{
*to = *from;
to++;
from++;
}
*to = '\0';
return 0;
}
void main41()
{
int ret = 0;
char buf1[] = "zbcdefg";
char buf2[1024] = { 0 };
ret = my_strcpy(buf2, buf1);
if (ret != 0)
{
switch (ret)
{
case 1:
printf("源buf出错!\n");
break;
case 2:
printf("目的buf出错!\n");
break;
case 3:
printf("copy过程出错!\n");
break;
default:
printf("未知错误!\n");
break;
}
}
printf("buf2:%s \n", buf2);
cout << "hello..." << endl;
system("pause");
return;
}
这个主要将的是throw抛出异常的类型和catch中的接收方式,总之,对于对象类型,使用引用是最安全的,因为c++编译器替我们接管了引用。