由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 为什么用try catch不住exception?
相关主题
C++ 的 exception handling几个问题
一道编程题 - throw Exceptionsc++的问题
如何在VC++下把raw图像快速写到硬盘里呢?请教一下,exception时,destructor一定会被调用么?
effective C++里的memory pool 一问:请问C++ exception后如何清理function stack上的内存资源?
Question about a TICPP example请教一个c++ throw exception 问题
why do we still use dynamic allocation?关于内存泄漏
为什么有点函数声明的参数类型但是没有变量名呢?alloc这个函数究竟做些啥活呢?
C++ try {} catch(...){} 能扑捉一切异常吗?array allocation in c
相关话题的讨论汇总
话题: exception话题: catch话题: try话题: win32话题: __
进入Programming版参与讨论
1 (共1页)
u****u
发帖数: 229
1
比方这么一段程序:
int a=1, b=0,c;
int *p;
try {
c = a / b ;
p = new int [300000000]; //3Gmemory
}
catch (...)
{
cout << "error";
}
应该出现两个exception,一个是divided by zero,一个是failed to allocate memory,
本意是应该让catch 来输出出错,并不中断程序执行。
但是编出来的程序不能catch任何一个exception,程序还是会被中断。这到底是什么原因
呢?哪位高手指教一下吧。
环境:windows, bcc5, console application.
P***t
发帖数: 1006
2
For divide by zero, on Win32 system, there will be an Win32 Exception throwed,
and
it can't be catched by C++ exception handler. However, you can use:
__try
{
...
}
__except( EXCEPTION_EXECUTE_HANDLER )
{
}
with VC to catch Win32 exception. See help of __try.
For the second new stuff, it really depends on the implementation of "new".
But normally you should be able to catch it though. Make the number bigger.
I've tried 0x3ffffff0 and it works for me.
These things are compiler/system

【在 u****u 的大作中提到】
: 比方这么一段程序:
: int a=1, b=0,c;
: int *p;
: try {
: c = a / b ;
: p = new int [300000000]; //3Gmemory
: }
: catch (...)
: {
: cout << "error";

P***t
发帖数: 1006
3
"new" is just an operator and can be easily overloaded. You may trace into it
to see
how it is implemented. On my machine's setup, it's defined with STL, and looks
like:
void *__cdecl operator new(size_t size) _THROW1(_STD bad_alloc)
{ // try to allocate size bytes
void *p;
while ((p = malloc(size)) == 0)
if (_callnewh(size) == 0)
_STD _Nomemory();
return (p);
}
And _Nomemory is:
_CRTIMP2 void __cdecl _Nomemory()
{ // report out of memory
static const _XSTD bad_alloc nomem;
_RAISE(nomem);
}
#def

【在 u****u 的大作中提到】
: 比方这么一段程序:
: int a=1, b=0,c;
: int *p;
: try {
: c = a / b ;
: p = new int [300000000]; //3Gmemory
: }
: catch (...)
: {
: cout << "error";

1 (共1页)
进入Programming版参与讨论
相关主题
array allocation in cQuestion about a TICPP example
"brk()" 和 mmap() 有什么区别? (转载)why do we still use dynamic allocation?
C++一个string的小问题为什么有点函数声明的参数类型但是没有变量名呢?
菜鸟请教C问题C++ try {} catch(...){} 能扑捉一切异常吗?
C++ 的 exception handling几个问题
一道编程题 - throw Exceptionsc++的问题
如何在VC++下把raw图像快速写到硬盘里呢?请教一下,exception时,destructor一定会被调用么?
effective C++里的memory pool 一问:请问C++ exception后如何清理function stack上的内存资源?
相关话题的讨论汇总
话题: exception话题: catch话题: try话题: win32话题: __