<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">[swift-evolution to bcc]</div><div class=""><br class=""></div><div class="">Hi, Andrew. Objective-C exceptions are not the same as Swift errors (the things that are thrown); they have effectively come to indicate fatal conditions in Apple’s frameworks, and neither Objective-C nor Swift are compiled exception-safe in the C++ sense.* (That is, if an Objective-C exception is raised, the program will leak, and some objects may be in an invalid state.)</div><div class=""><br class=""></div><div class="">Swift errors correspond to the Cocoa NSError idiom, and the latter is imported as such.</div><div class=""><br class=""></div><div class="">Separately, issues with Cocoa APIs should be filed as bug reports with Apple, rather than raised on the <a href="http://swift.org" class="">swift.org</a> mailing lists or bug tracker. The Swift Open Source project does not control Apple’s APIs, and the features which <i class="">do</i>&nbsp;affect particular frameworks (the SDK “overlays” and “API notes”) come from the particular framework teams within Apple.</div><div class=""><br class=""></div><div class="">Best,</div><div class="">Jordan</div><div class=""><br class=""></div><div class="">* Objective-C can be compiled in an exception-safe mode, but it’s off by default. I don’t know if Apple’s frameworks are compiled in this mode.</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 11, 2016, at 20:07, Andrew Tetlaw via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Apologies if this has been discussed, I couldn't find any mention in the list or on <a href="https://github.com/apple/swift-3-api-guidelines-review" class="">https://github.com/apple/swift-3-api-guidelines-review</a>.<br class=""><br class="">The NSIncrementalStore method:<br class=""><br class="">&nbsp; &nbsp; func referenceObjectForObjectID(_ objectID: NSManagedObjectID) -&gt; AnyObject<br class=""><br class="">Can thrown an exception, but it's not marked as throws. From the documentation:<br class=""><br class="">"This method raises an NSInvalidArgumentException if the object ID was not created by the receiving store."<br class=""><br class="">NSExpression has a constantValue property:<div class=""><br class=""></div>&nbsp; &nbsp; var constantValue: AnyObject { get }<div class=""><br class=""></div><div class="">that can raise an exception at runtime, but it's not marked as throws. From the documentation:<br class=""><br class="">'Accessing this property raises an exception if it is not applicable to the expression."<br class=""><br class="">The NSExpression constantValue property can also return nil at runtime, but it's return type is AnyObject; not an optional.<br class=""><br class="">If you construct an NSComparisonPredicate with a format string like:<br class=""><br class="">&nbsp; &nbsp; "property == nil"<br class=""><br class="">and you examine the expression returned by predicate.rightExpression. You can't call expression.constantValue from Swift because it'll crash with EXC_BAD_ACCESS. You also can't protect against a nil value because the return type is not optional.<br class=""><br class=""><br class="">To protect Swift against both of these situations, you have to use Objective-C wrapper functions. For example to protect against exceptions:</div><div class=""><br class=""></div>NSException *_Nullable IADCatchObjCException(void (^_Nullable block)())<br class="">{<br class="">&nbsp; &nbsp; NSException *exception = nil;<br class="">&nbsp; &nbsp; @try {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; if (block) {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; block();<br class="">&nbsp; &nbsp; &nbsp; &nbsp; }<br class="">&nbsp; &nbsp; } @catch (NSException *e) {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; exception = e;<br class="">&nbsp; &nbsp; }<br class="">&nbsp; &nbsp; return exception;<br class="">} <div class=""><br class=""></div><div class="">To protect against a surprise nil at runtime:</div><div class=""><br class=""></div>inline _Nullable id IADMaybeNilValue(_Nullable id value)<br class="">{<br class="">&nbsp; &nbsp; return value ?: nil;<br class="">}<div class=""><br class=""></div><div class="">I was hoping this could be improved for the Swift API?</div><div class=""><br class="">--<br class="">Andrew Tetlaw<br class=""><div class=""><div class="">
</div></div></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>