关于C++异常抛出问题
用throw抛出一个常量字符串,我验证过后发现这个常量字符串无法抛出,且一直报错,希望大神解答
#include <iostream>
using namespace std;
void My_Strcpy(char* des, char* src) {
if (des == NULL) {
throw "拷贝目的出错";
}
if (src == NULL) {
throw "拷贝源出错";
}
/*
实验失败,用throw抛出的常量字符串根本无法抛出
失败原因未知
*/
if (*src == 'a') {
throw "拷贝过程出错";
}
while (*src !='\0') {
*des = *src;
des++;
src++;
}
*des = '\0';
}
int main() {
char buf1[] = "anndwoa";
char buf2[1024] = { 0 };
try
{
My_Strcpy(buf2, buf1);
}
catch (int e)
{
cout << e << "int类型错误" << endl;
}
catch (char *e)
{
cout << e << "char*类型错误" << endl;
}
printf("%s \n", buf2);
cout<<"hello....."<<endl;
system("pause");
return 0;
}