Re: Class throwing exceptions in aggregate static initializer causes
abort() ...
* Marvin Barley:
I have a class that throws exceptions in new initializer, and a static
array of objects of this type.
When something is wrong in initialization, CGI program crashes
miserably. Debugging shows uncaught exception.
How to catch an exception that happened before main() try { ... }
catch (...) { ... } block?
Is there a way?
Locally yes, just use try-catch; globally, in a portable way, no.
That said, what you have doesn't seem to be exceptions resulting from
exceptional circumstances (such as resource shortage), but resulting
from programming bugs.
Fix your code and you won't have that problem.
Thank you very much in forward,
Marvin
class formtblitem {
protected:
void clear() {
memset (form_field, 0, sizeof (form_field ));
memset (field_type, 0, sizeof (field_type ));
memset (dflt, 0, sizeof (dflt ));
memset (minval, 0, sizeof (minval ));
memset (maxval, 0, sizeof (maxval ));
memset (POVRAYsubst, 0, sizeof (POVRAYsubst));
varaddr = NULL;
}
memset is not guaranteed to yield C++ nullvalues, and is dangerous and
generally inefficient (when wielded by novices) -- not that you should
focus on effiency at all before having reasonably /correct/ code.
Here's a reasonable and correct implementation of clear(), assuming a
default constructor that clears:
void clear() { *this = formtblitem(); }
Don't use raw arrays (even though this code also works with raw arrays).
#define STRCPY(DEST, SRC) > if (strlen (SRC) > sizeof (DEST) - 1) > throw PARM_ESTRBUFOVERFLOW; > else > strcpy (DEST, SRC);
Don't use macros.
void initialize (const char *ff, const char *ft, const char
*df,
const char *mn, const char *mx, const char
*ps) {
Don't use raw pointers.
STRCPY (form_field, ff);
STRCPY (field_type, ft);
Don't use macros.
if (NotNull()) {
switch (FieldType()) {
case 'b':
if (!isbinary (df) || !isbinary (mn) || !
isbinary (mx))
throw PARM_EBINARY;
Reserve all uppercase names for macros (which you shouldn't use) --
see this group's FAQ and Bjarne Stroustrup's FAQ.
break;
case 'i':
if (!isinteger (df) || !isinteger (mn) || !
isinteger (mx))
throw PARM_EINTEGER;
break;
case 'f':
if (!isfloat (df) || !isfloat (mn) || !
isfloat (mx)
.
.
.
Additional errors probably in the declarations you left out.
};
typedef class formtblitem FTI;
No need to use keyword 'class' -- C'ism.
class formtblitem formtbl[] = {
No need to use keyword 'class' -- not even a C'ism.
// FORM field, type, dflt, min, max, POV-Ray subst
//
FTI ("debug", "b", "0", "0", "1", "DEBUG",
&debug ),
FTI ("rwidth", "i", "800", "16", "13000", "RWIDTH",
&rwidth ),
FTI ("rheight", "i", "600", "16", "13000", "RHEIGHT",
&rheight ),
FTI ("quality", "i", "9", "0", "13", "Q",
&quality ),
FTI ("antialiasing", "b", "0", "0", "1", "AA",
&antialiasing ),
.
.
.
};
It might be a good idea to reproduce in a small program that compiles,
and post that code.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?