首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

错误处理出错

2012-02-25 
异常处理出错处理动态分配失败时的异常,为什么出错了,应该怎么做,谢谢!!程序如下:#include iostream.h#i

异常处理出错
处理动态分配失败时的异常,为什么出错了,应该怎么做,谢谢!!程序如下:
#include <iostream.h>
#include <string.h>
int   main()
{
char   *p,aa[100];
//int   n;
cout < < "input   string: ";
cin> > aa;
//cout < < "input   integer   number: ";
//cin> > n;
try
{
p=new   char[strlen(aa)+1];
if(p==NULL)
throw   p;
else
{
strcpy(p,aa);
cout < <p < <endl;
}
}
catch(NULL)
{
cout < < "it 's   wrong! " < <endl;
}
return   0;
}


[解决办法]
楼主,你在用什么书学C++?catch的语法,上面没讲么?
另外,你可能不知道,new失败的时候是直接抛异常,而不是返回null
具体,去看《Effective C++》吧。
[解决办法]
扔了你手里那本书吧,换C++ Primer。
try
{
p=new char[strlen(aa)+1];
strcpy(p,aa);
cout < <p < <endl;
}
catch(...)
{
cout < < "it 's wrong! " < <endl;
}
[解决办法]
catch(NULL)

很彪悍的语法。。。

try {
int i = ...;
} catch ( 1 ) {
} catch ( 2 ) {
} ... ?

热点排行