Is it every appropriate to throw in a dtor? I am thinking about a simple
example of a wrapper around a POSIX file...
_______________ _______________ _______________ _______________ ____________
class file {
FILE* m_handle;
public:
// [...];
~file() /* throw() */ {
int const status fclose(m_handle );
if (status) {
/* shi% hit the fan:
/*
// [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?
<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_fro m_dtor const& e) {
// handle error from `e.get_status() '
delete f;
}
return 0;
}
_______________ _______________ _______________ _______________ ___
?
or what about using smart pointer...
int main() {
std::auto_ptr<f ilef;
try {
f.reset(new file());
} catch(throw_fro m_dtor const& e) {
// handle error from `e.get_status() '
}
}
?
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!!!
;^/
example of a wrapper around a POSIX file...
_______________ _______________ _______________ _______________ ____________
class file {
FILE* m_handle;
public:
// [...];
~file() /* throw() */ {
int const status fclose(m_handle );
if (status) {
/* shi% hit the fan:
/*
// [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?
<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_fro m_dtor const& e) {
// handle error from `e.get_status() '
delete f;
}
return 0;
}
_______________ _______________ _______________ _______________ ___
?
or what about using smart pointer...
int main() {
std::auto_ptr<f ilef;
try {
f.reset(new file());
} catch(throw_fro m_dtor const& e) {
// handle error from `e.get_status() '
}
}
?
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!!!
;^/
Comment