<div dir="ltr">You could use a result type, e.g.:<div><br></div><div><a href="https://github.com/antitypical/Result">https://github.com/antitypical/Result</a><br></div><div><br></div><div>Or roll your own result type.</div><div><br></div><div>I think the reason that Swift doesn&#39;t support what you want is because of rethrows. When you declare a method as rethrows it can throw anything because the closure it is re-throwing can throw anything. They could have fully typed the rethrows but obviously decided that was not worth it. At present rethrows is light weight; the compiler generates two versions of the method, one that throws and one that doesn&#39;t. If it was typed then it would be like a generic method and a version of the method would be required for each type combination that was actually used.</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature">  -- Howard.<br></div></div>
<br><div class="gmail_quote">On 6 July 2017 at 10:38, Tim Wang via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Swifters,<div><br></div><div>I am wondering if it is possible to specify error types thrown in a protocol method. By allowing us to do it and letting the compiler check all the implementations of the protocol to make sure only they would only throw the specified error types, we only need to catch our error types when calling these methods. </div><div><br></div><div>





For the code below:<br><br>





<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-s1">enum</span> MyError: <span class="m_-8446920124172038182inbox-inbox-s2">Error</span> {</p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">    </span><span class="m_-8446920124172038182inbox-inbox-s1">case</span> justError</p>
<p class="m_-8446920124172038182inbox-inbox-p1">}</p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-s1">protocol</span> MethodWillThrow {</p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">    </span><span class="m_-8446920124172038182inbox-inbox-s1">func</span> testMethod() <span class="m_-8446920124172038182inbox-inbox-s1">throws</span> MyError</p>
<p class="m_-8446920124172038182inbox-inbox-p1">}</p>
<p class="m_-8446920124172038182inbox-inbox-p2"><br></p>
<p class="m_-8446920124172038182inbox-inbox-p3"><span class="m_-8446920124172038182inbox-inbox-s1">extension</span><span class="m_-8446920124172038182inbox-inbox-s3"> </span>MethodThrow<span class="m_-8446920124172038182inbox-inbox-s3"> {</span></p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">    </span><span class="m_-8446920124172038182inbox-inbox-s1">func</span> testMethod() <span class="m_-8446920124172038182inbox-inbox-s1">throws</span> {</p>
<p class="m_-8446920124172038182inbox-inbox-p3"><span class="m_-8446920124172038182inbox-inbox-s3"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">        </span></span><span class="m_-8446920124172038182inbox-inbox-s1">throw</span><span class="m_-8446920124172038182inbox-inbox-s3"> </span>MyError<span class="m_-8446920124172038182inbox-inbox-s3">.</span>justError</p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">    </span>}</p>
<p class="m_-8446920124172038182inbox-inbox-p1">}</p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-s1">class</span> TestClass: <span class="m_-8446920124172038182inbox-inbox-s4">MethodThrow</span> {</p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">    </span><span class="m_-8446920124172038182inbox-inbox-s1">func</span> testMethod() <span class="m_-8446920124172038182inbox-inbox-s1">throws</span> {</p>
<p class="m_-8446920124172038182inbox-inbox-p3"><span class="m_-8446920124172038182inbox-inbox-s3"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">        </span></span><span class="m_-8446920124172038182inbox-inbox-s1">throw</span><span class="m_-8446920124172038182inbox-inbox-s3"> </span>MyError<span class="m_-8446920124172038182inbox-inbox-s3">.</span>justError</p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">    </span>}</p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">    </span><span class="m_-8446920124172038182inbox-inbox-s1">func</span> anotherMethod() {</p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">        </span><span class="m_-8446920124172038182inbox-inbox-s1">do</span> {</p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">            </span><span class="m_-8446920124172038182inbox-inbox-s1">try</span> <span class="m_-8446920124172038182inbox-inbox-s4">testMethod</span>()</p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">        </span>} <span class="m_-8446920124172038182inbox-inbox-s1">catch</span> MyError.<span class="m_-8446920124172038182inbox-inbox-s4">justError</span> {</p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">            </span><span class="m_-8446920124172038182inbox-inbox-s2">print</span>(<span class="m_-8446920124172038182inbox-inbox-s5">&quot;my error&quot;</span>)</p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">        </span>} <b><span class="m_-8446920124172038182inbox-inbox-s1">catch</span> {</b></p>
<p class="m_-8446920124172038182inbox-inbox-p1"><b><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">            </span><span class="m_-8446920124172038182inbox-inbox-s2">print</span>(<span class="m_-8446920124172038182inbox-inbox-s5">&quot;other error&quot;</span>)</b></p>
<p class="m_-8446920124172038182inbox-inbox-p1"><b><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">        </span>}</b></p>
<p class="m_-8446920124172038182inbox-inbox-p1"><span class="m_-8446920124172038182inbox-inbox-Apple-converted-space">    </span>}</p>
<p class="m_-8446920124172038182inbox-inbox-p1">}</p><p class="m_-8446920124172038182inbox-inbox-p1">Now we need add this extra default catch to make it compile and work and I really want to remove this catch.</p><p class="m_-8446920124172038182inbox-inbox-p1">Please let me know if there is a way to do it and thanks for help in advance.<span class="HOEnZb"><font color="#888888"><br></font></span></p><span class="HOEnZb"><font color="#888888"><p class="m_-8446920124172038182inbox-inbox-p1">Tim Wang</p></font></span></div></div>
<br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>