Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -296,10 +296,20 @@ /** * An OFException indicating an attempt to connect or bind an already connected * or bound socket */ @interface OFAlreadyConnectedException: OFException {} +/** + * \return An error message for the exception as a C string. + */ +- (char*)cString; +@end + +/** + * An OFException indicating that the specified port is invalid. + */ +@interface OFInvalidPortException: OFException /** * \return An error message for the exception as a C string. */ - (char*)cString; @end Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -320,8 +320,22 @@ return string; asprintf(&string, "The socket of type %s is already connected or bound " "and thus can't be connected or bound again!", [object name]); + return string; +} +@end + +@implementation OFInvalidPortException +- (char*)cString +{ + if (string != NULL) + return string; + + asprintf(&string, "The port specified is not valid for a socket of " + "type %s! This usually means you tried to use port 0, which is an " + "invalid port.", [object name]); + return string; } @end Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -59,14 +59,12 @@ onPort: (uint16_t)port { struct addrinfo hints, *res, *res0; char portstr[6]; - if (!port) { - /* FIXME: Throw exception */ - return nil; - } + if (!port) + @throw [OFInvalidPortException newWithObject: self]; if (sock >= 0) @throw [OFAlreadyConnectedException newWithObject: self]; memset(&hints, 0, sizeof(struct addrinfo)); @@ -109,14 +107,12 @@ andFamily: (int)family { struct addrinfo hints, *res; char portstr[6]; - if (!port) { - /* FIXME: Throw exception */ - return nil; - } + if (!port) + @throw [OFInvalidPortException newWithObject: self]; if (sock >= 0) @throw [OFAlreadyConnectedException newWithObject: self]; if ((sock = socket(family, SOCK_STREAM, 0)) < 0) {