0

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];
        }
    }
}
4
  • 3
    Note that this is not related to Xcode. You could use any IDE, any compiler, whatever - the question is about Objective-C exceptions. Commented May 5, 2013 at 5:09
  • Show us the code where you throw the exception Commented May 5, 2013 at 5:13
  • for the above code, where do I put the throw before I can try and catch? Commented May 5, 2013 at 5:17
  • 2
    Exceptions in Objective-C are only for exceptional circumstances, and not for regular program flow. They almost always indicate programmer error. You should ideally never have to use at-try and at-catch in an ObjC application, and not in this case either. Commented May 5, 2013 at 6:09

1 Answer 1

2

Exception Handling will not solve your problem here,

If you call - (void) playSound:(NSUInteger) number withDelay:(NSTimeInterval) delay from button press events. Multiple button presses would lead to this kind of crashes. Because you are using the same AVAudioPlayer variable *sound and that same object is used to play sounds after delays.A call from other button press could be initiating the sound player, while another one tries to play the sound.

If these are short sound clips (less than 30sec), Without using AVAudioPlayer , you better use AudioServicesPlaySystemSound,

You can write a method to delay the start of play, This will let you play multiple sound clips at the same time without any problem.

NSString *path = [[NSBundle mainBundle] pathForResource:@"Shuffle" ofType:@"caf"];
NSURL *url = [NSURL fileURLWithPath:path];
SystemSoundID  soundFileObject;
AudioServicesCreateSystemSoundID ((__bridge_retained CFURLRef) url, &soundFileObject);
AudioServicesPlaySystemSound (soundFileObject);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.