Redian新闻
>
为什么用try catch不住exception?
avatar
为什么用try catch不住exception?# Programming - 葵花宝典
u*u
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.
avatar
P*t
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";

avatar
P*t
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";

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。