| 1 | #import <Foundation/Foundation.h> |
| 2 | |
| 3 | #import <exception> |
| 4 | #import <stdexcept> |
| 5 | |
| 6 | @interface MyCustomException: NSException |
| 7 | @end |
| 8 | @implementation MyCustomException |
| 9 | @end |
| 10 | |
| 11 | void 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 | |
| 24 | void rethrow(int n) |
| 25 | { |
| 26 | @try { |
| 27 | foo(n); |
| 28 | } @catch(NSException *e) { |
| 29 | @throw; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | int 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(n: atoi(nptr: argv[1])); |
| 43 | } @catch(NSException *e) { |
| 44 | e2 = e; |
| 45 | } |
| 46 | |
| 47 | NSLog(@"1" ); // Set break point at this line. |
| 48 | |
| 49 | rethrow(n: atoi(nptr: argv[1])); |
| 50 | |
| 51 | [pool drain]; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | |