Michael Ozeryansky
Michael Ozeryansky

Reputation: 8033

Explain why NSURL throws errors on nil

Most to all of the classes in Objective-C returns nil if passed nil or some error, but NSURL throws an exception. Specifically, the method [NSURL fileURLWithPath].

It is documented: "Passing nil for this parameter produces an exception."

But, can anyone explain Apple would throw an exception instead of returning nil?

Upvotes: 5

Views: 233

Answers (2)

Michael Ozeryansky
Michael Ozeryansky

Reputation: 8033

Here is Apple's docs or error handling:

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/ErrorHandling/ErrorHandling.html

exceptions are used solely for programmer errors

Upvotes: 0

CRD
CRD

Reputation: 53000

It is calling a method on nil that is allowed. Many methods can throw exceptions if given an invalid argument. For example see NSMutableArray's addObject:.

Upvotes: 1

Related Questions