Re: Exception handling problem
On Wednesday, July 3, 2013 10:29:43 PM UTC+1, woodb...@gmail.com wrote:
On Wednesday, July 3, 2013 9:18:30 PM UTC, Victor Bazarov wrote:
You're supposed to catch what's thrown. I didn't see the code that
throws. Did you write it? Or does it come from the system?
int cmw::sockRead (sock_type sock, void* data, int len
, sockaddr* fromAddr, socklen_t* fromLen)
{
int rc = recvfrom(sock
, static_cast<char*> (data)
, len
, 0
, fromAddr
, fromLen
);
if (rc < 0) {
auto err = GetError();
if (ECONNRESET == err) {
throw eof("sockRead -- ECONNRESET");
}
throw failure("sockRead -- len: ") << len << " errno: " << err;
} else {
if (rc == 0) {
throw eof("sockRead -- rc == 0. len: ") << len; // <-- this line
}
return rc;
}
}
Sorry. The line with the comment throws. I get the
same message/what string/ either way and I've checked
that that message is only thrown in this one place.
What is the type returned from "eof( ... ) << len"? If the
operator<< is defined in failure, and it returns a failure&,
then what will be thrown is a failure, not an eof. The type
thrown is the *static* type of the expression, by copy (and
thus, with possible slicing). If for some reason, you need to
throw the dynamic type, the solution is to provide a virtual
member function (say raise), and overload that in all of the
derived classes to do the throw there (where the dynamic type
has been resolved). Although in your case, that would lead to
some mighty strange syntax:
(eof( "..." ) << len).raise();
Alternatively, you can use a global function:
raise( eof( "..." ) << len );
which is less awkward.
--
James