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";
|
|