处理objective-c中的nil异常

1 cocoa-touch exception objective-c

嘿所有我正在使用MPMoviePlayerController并尝试在没有电影时捕捉我的异常.

movies = [movieDictionary objectForKey:@"movieID"];

NSLog(@"callVideoSetting");
CGRect playfram = CGRectMake(0, 0, 320, 500);

stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
[stopButton setFrame:playfram];
[stopButton addTarget:self action:@selector(stopVideo:) forControlEvents:UIControlEventTouchUpInside];

NSURL *movie = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:movies ofType:@"m4v"]];
//path for resource moet id worden 
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movie];
[theMovie setOrientation:UIDeviceOrientationPortrait animated:NO];
Run Code Online (Sandbox Code Playgroud)

现在我希望在电影背后没有任何内容时抓住异常.

- [NSURL initFileURLWithPath:]:nil string parameter'

我知道我应该尝试一下catch块.但是我从未处理过objective-c中的异常.

Phi*_*ert 5

只需使用变量:

NSString * path = [[NSBundle mainBundle] pathForResource:movies ofType:@"m4v"];

if (!path) {
  // error handling
}

NSURL *movie = [NSURL fileURLWithPath:path];
Run Code Online (Sandbox Code Playgroud)