FROM : Todd Yandell
DATE : Fri Aug 25 21:06:52 2006
On Aug 25, 2006, at 12:38 PM, Roland Silver wrote:
> Can someone suggest a tutorial or example of the use of @try,
> @catch, @throw, @finally, in Objective-C, other than the Apple
> document "Exception Handling and Thread Synchronization"?
NSString *stringOne = @"One";
NSString *stringTwo = @"Two";
@try {
[stringOne appendString:stringTwo];
} @catch (id theException) {
NSLog(@"%@ doesn't respond to appendString:!", [stringOne class]);
} @finally {
NSLog(@"This always happens.");
}
@throw [NSException exceptionWithName:@"Test"
reason:@"Testing the @throw directive." userInfo:nil];
In the "@catch" block, "theException" will point to an NSException
that describes the exception that was caught. The "@finally" block
will always be executed, whether an exception was thrown or not.
Normally you would use "@finally" to clean up any objects you created
in the try "@try" block.
The "@throw" directive does the same thing as "[myException throw]".
You also need to enable these options in your build settings. In
Xcode, check the "Enable Objective-C Exceptions" box in your target's
build settings. With GCC, you need to use the "-fobjc-exceptions" flag.
Todd
DATE : Fri Aug 25 21:06:52 2006
On Aug 25, 2006, at 12:38 PM, Roland Silver wrote:
> Can someone suggest a tutorial or example of the use of @try,
> @catch, @throw, @finally, in Objective-C, other than the Apple
> document "Exception Handling and Thread Synchronization"?
NSString *stringOne = @"One";
NSString *stringTwo = @"Two";
@try {
[stringOne appendString:stringTwo];
} @catch (id theException) {
NSLog(@"%@ doesn't respond to appendString:!", [stringOne class]);
} @finally {
NSLog(@"This always happens.");
}
@throw [NSException exceptionWithName:@"Test"
reason:@"Testing the @throw directive." userInfo:nil];
In the "@catch" block, "theException" will point to an NSException
that describes the exception that was caught. The "@finally" block
will always be executed, whether an exception was thrown or not.
Normally you would use "@finally" to clean up any objects you created
in the try "@try" block.
The "@throw" directive does the same thing as "[myException throw]".
You also need to enable these options in your build settings. In
Xcode, check the "Enable Objective-C Exceptions" box in your target's
build settings. With GCC, you need to use the "-fobjc-exceptions" flag.
Todd
| Related mails | Author | Date |
|---|---|---|
| Roland Silver | Aug 25, 19:38 | |
| Todd Yandell | Aug 25, 21:06 | |
| Roland Silver | Aug 26, 00:38 | |
| Keith Ray | Aug 26, 01:10 | |
| Shawn Erickson | Aug 26, 01:24 | |
| Chris Hanson | Aug 26, 04:39 | |
| Gerriet M. Denkman… | Aug 26, 09:02 | |
| Cameron Hayne | Aug 26, 11:03 | |
| Roland Silver | Aug 26, 13:31 | |
| Chris Hanson | Aug 27, 00:59 | |
| Roland Silver | Aug 27, 14:21 | |
| j o a r | Aug 27, 15:06 |






Cocoa mail archive

