g++不支持异常处理 ?

yangrunhua 2000-02-15 05:11:00
有如下:
try
{
char *buffer=new char[1000000000];
if (buffer==NULL)
throw "out of memory";
}
catch(char*string)
{
cout<<string<<endl;
}
运行时竟然结果不是输出out of memory而是Aborted(core dumped)
Why?
...全文
410 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenshuishui 2001-05-31
  • 打赏
  • 举报
回复
同意jansenzhu(),直接试一下
try
{
throw "out of memory";
}
catch(char*string)
{
cout<<string<<endl;
}


skt642 2001-05-31
  • 打赏
  • 举报
回复
67017关注!
茂奇软件 2000-02-20
  • 打赏
  • 举报
回复
[1000000000];
perhaps the interage have overfollow.

you can try it directly.

try
{
throw "out of memory";
}
catch(bad_alloc e) //也可以catch(exception e),e.what()报的信息不同
{
cout<<e.what()<<endl;
}
catch(char*string)
{
cout<<string<<endl;
}


netmare 2000-02-16
  • 打赏
  • 举报
回复
我在linux下试了一下,sintony说的有理,是new时系统throw的异常.
netmare 2000-02-16
  • 打赏
  • 举报
回复
在linux下将程序改为
#include <exception> //注意是exception而不是exception.h
#include <new>

try
{
char *buffer=new char[1000000000];
if (buffer==NULL)
throw "out of memory";
}
catch(bad_alloc e) //也可以catch(exception e),e.what()报的信息不同
{
cout<<e.what()<<endl;
}
catch(char*string)
{
cout<<string<<endl;
}
sintony 2000-02-15
  • 打赏
  • 举报
回复
因为char *buffer=new char[1000000000];
本身就throw "Aborted(core dumped)"
你catch 的是系统的出错信息
netmare 2000-02-15
  • 打赏
  • 举报
回复
core dump可能发生在catch之后,因为cout是带缓冲的,所以未显示出来,用cerr试试.
从你的程序看,catch时除了显示错误信息后未作其他处理,若在catch之后又使用了buffer就会core dump了

19,610

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧