55 lines · plain
1#import <Foundation/Foundation.h>2 3#import <exception>4#import <stdexcept>5 6@interface MyCustomException: NSException7@end8@implementation MyCustomException9@end10 11void foo(int n)12{13 NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:@"some_value", @"some_key", nil];14 switch (n) {15 case 0:16 @throw [[NSException alloc] initWithName:@"ThrownException" reason:@"SomeReason" userInfo:info];17 case 1:18 @throw [[MyCustomException alloc] initWithName:@"ThrownException" reason:@"SomeReason" userInfo:info];19 case 2:20 throw std::runtime_error("C++ exception");21 }22}23 24void rethrow(int n)25{26 @try {27 foo(n);28 } @catch(NSException *e) {29 @throw;30 }31}32 33int main(int argc, const char * argv[])34{35 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];36 37 NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:@"some_value", @"some_key", nil];38 NSException *e1 = [[NSException alloc] initWithName:@"ExceptionName" reason:@"SomeReason" userInfo:info];39 NSException *e2;40 41 @try {42 foo(atoi(argv[1]));43 } @catch(NSException *e) {44 e2 = e;45 }46 47 NSLog(@"1"); // Set break point at this line.48 49 rethrow(atoi(argv[1]));50 51 [pool drain];52 return 0;53}54 55