Index: src/OFDictionary.m ================================================================== --- src/OFDictionary.m +++ src/OFDictionary.m @@ -111,26 +111,26 @@ { uint32_t hash = [key hash] & (size - 1); of_list_object_t *iter; if (data[hash] == nil) - return nil; + @throw [OFNotInSetException newWithClass: isa]; for (iter = [data[hash] first]; iter != NULL; iter = iter->next->next) if ([iter->object isEqual: key]) return iter->next->object; - return nil; + @throw [OFNotInSetException newWithClass: isa]; } - remove: (OFObject*)key { uint32_t hash = [key hash] & (size - 1); of_list_object_t *iter; if (data[hash] == nil) - return self; // FIXME: Throw exception? + @throw [OFNotInSetException newWithClass: isa]; for (iter = [data[hash] first]; iter != NULL; iter = iter->next->next) { if ([iter->object isEqual: key]) { [data[hash] remove: iter->next]; [data[hash] remove: iter]; @@ -137,15 +137,15 @@ return self; } } - return self; // FIXME: Throw exception? + @throw [OFNotInSetException newWithClass: isa]; } /* FIXME: Implement this! */ /* - (BOOL)isEqual { } */ @end Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -210,11 +210,17 @@ @end /** * An OFException indicating that initializing something failed. */ -@interface OFInitializationFailedException: OFException +@interface OFInitializationFailedException: OFException {} +@end + +/** + * An OFException indicating that the requested key is not in the set. + */ +@interface OFNotInSetException: OFException {} @end /** * An OFException indicating the file couldn't be opened. */ Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -267,10 +267,23 @@ if (string != NULL) return string; asprintf(&string, "Initialization failed for class %s!", [class name]); + return string; +} +@end + +@implementation OFNotInSetException +- (const char*)cString +{ + if (string != NULL) + return string; + + asprintf(&string, "The requested key is not in the set of type %s!", + [class name]); + return string; } @end @implementation OFOpenFileFailedException Index: tests/OFDictionary/OFDictionary.m ================================================================== --- tests/OFDictionary/OFDictionary.m +++ tests/OFDictionary/OFDictionary.m @@ -16,14 +16,17 @@ #import "OFAutoreleasePool.h" #import "OFDictionary.h" #import "OFConstString.h" #import "OFString.h" +#import "OFExceptions.h" int main() { + BOOL caught; + OFDictionary *dict = [OFDictionary dictionaryWithHashSize: 16]; OFAutoreleasePool *pool = [OFAutoreleasePool new]; OFString *key1 = [OFString stringWithCString: "key1"]; OFString *key2 = [OFString stringWithCString: "key2"]; @@ -35,17 +38,40 @@ [dict set: key2 to: value2]; [pool release]; if (strcmp([[dict get: @"key1"] cString], "value1")) { - puts("\033[K\033[1;31mTest 1/2 failed!\033[m"); + puts("\033[K\033[1;31mTest 1/4 failed!\033[m"); return 1; } if (strcmp([[dict get: key2] cString], "value2")) { - puts("\033[K\033[1;31mTest 2/2 failed!\033[m"); + puts("\033[K\033[1;31mTest 2/4 failed!\033[m"); + return 1; + } + + caught = NO; + @try { + [dict get: @"key3"]; + } @catch (OFNotInSetException *e) { + caught = YES; + } + if (!caught) { + puts("\033[K\033[1;31mTest 3/4 failed!\033[m"); + return 1; + } + + [dict remove: @"key2"]; + caught = NO; + @try { + [dict remove: @"key2"]; + } @catch (OFNotInSetException *e) { + caught = YES; + } + if (!caught) { + puts("\033[K\033[1;31mTest 4/4 failed!\033[m"); return 1; } - puts("\033[1;32mTests successful: 2/2\033[0m"); + puts("\033[1;32mTests successful: 4/4\033[0m"); return 0; }