I'm trying to figure out how exception handling works in Objective-C. So I'm forcing an exception by performing a selector (boom) which does not exist. When I run it, the application crushes, the exception is not handled properly. Can somebody show me the proper way of handling exceptions in objective-c?
@try{
[self performSelector:@selector(boom)];
}
@catch (NSException *ex) {
NSLog(@"Error");
}
... And can somebody also show me how to handle exception for the code below. I'm using one instance variable for four different sound effects. After playing, I set the variable to nil. For some reason, the code below crushes the app at times, so I will like to handle that exception. Thanks.
- (void) playSound:(NSUInteger) number withDelay:(NSTimeInterval) delay
{
if(sound == nil)
{
NSURL* url;
switch (number)
{
case 1: url = [[NSBundle mainBundle] URLForResource:@"Shuffle" withExtension:@"caf"]; break;
case 3: url = [[NSBundle mainBundle] URLForResource:@"Clap" withExtension:@"caf"]; break;
case 4: url = [[NSBundle mainBundle] URLForResource:@"Glass" withExtension:@"caf"]; break;
case 5: url = [[NSBundle mainBundle] URLForResource:@"Knock" withExtension:@"caf"]; break;
default: break;
}
if (url != nil)
{
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[sound setCurrentTime:0];
[sound prepareToPlay];
[sound setVolume:1.0];
[sound performSelector:@selector(play) withObject:nil afterDelay: delay];
}
}
}