Exception handler
Tsvilikhovskiy_Jake/ny_mis@npd.com
Monday, May 20, 1996
Env: Win3.11 VC++ 1.51
Hi ppl!
My application(16 bit W3.11 medium memory model)
uses a lot of memory allocation.
There is a good chance of running out of memory.
I want to write a exception handler as I
TRY{
new something
CATCH(CMemoryException,e)
{
fExeptionHandler() // is supposed to to give an error message, free //
whatever is allocated and terminate the application
}
END_CATCH
This can happen in a lot of places in my program.
many different things can be allocated at this undefined point
of execution.
The question :
Where can I find info how to write such a handler?
As I underdstand I will need some kind of garbage collector
for that...
Any help is greatly appreciated
Jake
Dan L. Pierson -- dan@cthulhu.control.com
Wednesday, May 22, 1996
[Mini-digest: 3 responses]
You have four main choices:
1. Switch to a 32-bit version of VC++ which really implements exception handling.
2. Switch to a competitive 16-bit C++ compiler which supports both exception handling
and MFC, E.G. Symantec C++ 7.x. In this case you may need to use 'try', instead of "TRY", etc.
I don't know if this will break the 16-bit MFC.
3. Buy a third party garbage collector such as Great Circle from Geodesic Systems. (I don't know if
1.0 supports 16-bit VC++/MFC, but the current beta 2.0 claims to do so. BTW: I'm in early eval
stages with this, so can't be much help yet).
4. Give up on exception handling until you can do one of the above. The MFC 2.x macros that try
to look like exception handling are a far cry from the real thing. While I can understand the MFC
group wanting to use such a kludge internally, IMHO documenting them was a mistake. They
tend to fool unsuspecting customers into thinking they have something they don't...
-----From: "Frederic Steppe"
You said you want to terminate the application if the memory becomes too low.
Do it this way (a little bit radical) :
TRY
{
new something; // You may allocate more than one block in one try, but you
will not know which one has failed
new otherthing;
}
CATCH(CMemoryException,e)
{
fExceptionHandler(e);
}
END_CATCH
void fExceptionHandler(CException *e)
{
// Do whatever you want, but don't allocate anything !
AfxAbort(); // If you can't recover from the error, just exit
}
You better try to recover and exit the function, but not the application :
BOOL bInitOK = TRUE;
TRY
{
new something;
}
CATCH(CMemoryException,e)
{
free WhatWasAllocatedAndCanBeFreed;
fExceptionHandler(e, FALSE);
bInitOK = FALSE;
}
END_CATCH
void fExceptionHandler(CException *e, BOOL bFatal)
{
// Do whatever you want, but don't allocate anything !
if(bFatal)
AfxAbort(); // If you can't recover from the error, just exit
else
::MessageBox(NULL,"Insufficient memory to complete this operation.\nPlease
close one or more applications and try again.","Low Memory",MB_ICONSTOP |
MB_OK | MB_SYSTEMMODAL);
}
Frederic Steppe (frederics@msn.com)
-----From: "Julius Hamelberg"
Check out this site:
EXCEPTION HANDLING:
A FALSE SENSE OF SECURITY
http://www.aw.com/cp/mec++-cargill.html
I found it very interesting.
Hope this helps.
Julius
| Вернуться в корень Архива
|