<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><blockquote type="cite" class="">On Dec 31, 2017, at 1:35 AM, Nevin Brackett-Rozinsky via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:<br class=""></blockquote><blockquote type="cite" class=""><br class="">So, I know this doesn’t actually address your issue, but I think it is important to clarify that “rethrows” does not guarantee anything about the *type* of error thrown by a function. What “rethrows” implies is that the function *will not throw* unless at&nbsp;least one of its arguments throws.<br class=""><br class="">In particular, if the argument throws, the outer function can throw *any error or none at all*. For example,<br class=""><br class="">enum CatError: Error { case hairball }<br class="">enum DogError: Error { case chasedSkunk }<br class=""><br class="">func foo(_ f: () throws -&gt; Void) rethrows -&gt; Void {<br class="">&nbsp; &nbsp; do &nbsp; &nbsp;{ try f() }<br class="">&nbsp; &nbsp; catch { throw CatError.hairball }<br class="">}<br class=""><br class="">do {<br class="">&nbsp; &nbsp; try foo{ throw DogError.chasedSkunk }<br class="">} catch {<br class="">&nbsp; &nbsp; print(error) &nbsp; &nbsp;// hairball<br class="">}<br class=""><br class="">Inside foo’s catch block, it is legal to throw any error, or not throw an error at all. But *outside* that catch block foo cannot throw, which is causing you consternation.<br class=""><br class="">• • •<br class=""><br class="">I don’t have a good solution for you, but in attempting to find one I *did* uncover something which compiles that probably shouldn’t. It seems that a “rethrows” function is currently allowed to throw if a *local* function throws:<br class=""><br class="">func rethrowing(_ f: () throws -&gt; Void) rethrows -&gt; Void {<br class="">&nbsp; &nbsp; func localThrowing() throws -&gt; Void { throw CatError.hairball }<br class="">&nbsp; &nbsp; return try localThrowing()<br class="">}<br class=""><br class="">do {<br class="">&nbsp; &nbsp; try rethrowing{ throw DogError.chasedSkunk }<br class="">} catch {<br class="">&nbsp; &nbsp; print(error) &nbsp; &nbsp;// hairball<br class="">}<br class=""><br class="">I wouldn’t count on this functionality as it is most likely a bug. Indeed, if we pass in a non-throwing argument then we get a runtime error:<br class=""><br class="">rethrowing{ return } &nbsp;// EXC_BAD_ACCESS (code=1, address=0x0)<br class=""><br class="">Although, if we change “localThrowing” to use do/catch on a call to “f” and throw only in the catch block, or even use your “var caught: Error?” trick, then it appears to work as intended with no problems at runtime.<br class=""><br class="">• • •<br class=""><br class="">In the unlikely scenario that the above local-function behavior is valid and intended, the following function will, technically speaking, let you work around the issue you’re having:<br class=""><br class="">func withPredicateErrors &lt;Element, Return&gt;<br class="">&nbsp; &nbsp; (_ predicate: (Element) throws -&gt; Bool,<br class="">&nbsp; &nbsp; &nbsp;do body: @escaping ((Element) -&gt; Bool) -&gt; Return<br class="">&nbsp; &nbsp; ) rethrows -&gt; Return<br class="">{<br class="">&nbsp; &nbsp; func bodyWrapper(_ f: (Element) throws -&gt; Bool) throws -&gt; Return {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; var caught: Error?<br class="">&nbsp; &nbsp; &nbsp; &nbsp; let value = body{ elem in<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return try f(elem)<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; caught = error<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br class="">&nbsp; &nbsp; &nbsp; &nbsp; }<br class="">&nbsp; &nbsp; &nbsp; &nbsp; if let caught = caught { throw caught }<br class="">&nbsp; &nbsp; &nbsp; &nbsp; return value<br class="">&nbsp; &nbsp; }<br class=""><br class="">&nbsp; &nbsp; return try bodyWrapper(predicate)<br class="">}<br class=""><br class="">It is not pretty, and it probably relies on a compiler bug, but at the present time, against all odds, it look like this operates as you intend.<br class=""><br class="">Nevin<br class=""></blockquote><br class="">That’s not the only exploit of that sort to exist in the frameworks, actually—and if you look through the source code to the standard library, you’ll see that the Swift standard library actually uses this kind of thing from time to time. For example, take DispatchQueue.sync(),&nbsp;which is declared as ‘rethrows’ despite wrapping a C function that Swift can’t reason anything about. I’d always wondered how that worked, so at some point I looked it up. Here’s how it's implemented:<br class=""><br class="">The public function we’re all used to, sync():<br class=""><br class=""><font face="Menlo" class="">public&nbsp;func&nbsp;sync&lt;T&gt;(execute&nbsp;work: ()&nbsp;throws&nbsp;-&gt;&nbsp;T)&nbsp;rethrows&nbsp;-&gt;&nbsp;T {<br class="">&nbsp; &nbsp; return&nbsp;try&nbsp;self._syncHelper(fn: sync,&nbsp;execute: work,&nbsp;rescue: {&nbsp;throw&nbsp;$0&nbsp;})<br class="">}<br class=""></font><br class=""><div class="">This basically passes everything on to a private method, _syncHelper, which looks like this:</div><div class=""><br class=""><font face="Menlo" class="">private&nbsp;func&nbsp;_syncHelper&lt;T&gt;(<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fn: (()&nbsp;-&gt;&nbsp;Void)&nbsp;-&gt;&nbsp;Void,<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;execute&nbsp;work: ()&nbsp;throws&nbsp;-&gt;&nbsp;T,<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rescue: ((Error)&nbsp;throws&nbsp;-&gt;&nbsp;(T)))&nbsp;rethrows&nbsp;-&gt;&nbsp;T<br class="">{<br class="">&nbsp; &nbsp; var&nbsp;result:&nbsp;T?<br class="">&nbsp; &nbsp; var&nbsp;error:&nbsp;Error?<br class="">&nbsp; &nbsp; withoutActuallyEscaping(work) { _work&nbsp;in<br class="">&nbsp; &nbsp; &nbsp; &nbsp; fn {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do&nbsp;{<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result&nbsp;=&nbsp;try&nbsp;_work()<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;catch&nbsp;let&nbsp;e {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error&nbsp;=&nbsp;e<br class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br class="">&nbsp; &nbsp; &nbsp; &nbsp; }<br class="">&nbsp; &nbsp; }<br class="">&nbsp; &nbsp; if&nbsp;let&nbsp;e&nbsp;=&nbsp;error {<br class="">&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp;try&nbsp;rescue(e)<br class="">&nbsp; &nbsp; }&nbsp;else&nbsp;{<br class="">&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp;result!<br class="">&nbsp; &nbsp; }<br class="">}</font></div><div class=""><br class=""></div><div class="">So basically, since every error thrown inside _syncHelper() is rethrown from one of the closures passed into it, that satisfies ‘rethrows’, and it also satisfies ‘rethrows’ for sync(), since anything thrown by it is thrown by another method that’s also declared “rethrows”. Sneaky.</div><div class=""><br class=""></div><div class="">Anyway, if you really need to do something like this, I’d recommend doing it the way the Swift standard library does, because:</div><div class=""><br class=""></div><div class="">1. They can’t break that in the compiler without also breaking their own code, and:</div><div class=""><br class=""></div><div class="">2. If they *do* break it because they’ve introduced a new, proper way to do this using some kind of actuallyRethrows() function or something, you’ll want to switch to that anyway.</div><div class=""><br class=""></div><div class="">Charles</div><div class=""><br class=""></div></body></html>