<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="inbox-inbox-p1"><span class="inbox-inbox-s1">enum</span> MyError: <span class="inbox-inbox-s2">Error</span> {</p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-Apple-converted-space">    </span><span class="inbox-inbox-s1">case</span> justError</p>
<p class="inbox-inbox-p1">}</p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-s1">protocol</span> MethodWillThrow {</p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-Apple-converted-space">    </span><span class="inbox-inbox-s1">func</span> testMethod() <span class="inbox-inbox-s1">throws</span> MyError</p>
<p class="inbox-inbox-p1">}</p>
<p class="inbox-inbox-p2"><br></p>
<p class="inbox-inbox-p3"><span class="inbox-inbox-s1">extension</span><span class="inbox-inbox-s3"> </span>MethodThrow<span class="inbox-inbox-s3"> {</span></p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-Apple-converted-space">    </span><span class="inbox-inbox-s1">func</span> testMethod() <span class="inbox-inbox-s1">throws</span> {</p>
<p class="inbox-inbox-p3"><span class="inbox-inbox-s3"><span class="inbox-inbox-Apple-converted-space">        </span></span><span class="inbox-inbox-s1">throw</span><span class="inbox-inbox-s3"> </span>MyError<span class="inbox-inbox-s3">.</span>justError</p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-Apple-converted-space">    </span>}</p>
<p class="inbox-inbox-p1">}</p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-s1">class</span> TestClass: <span class="inbox-inbox-s4">MethodThrow</span> {</p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-Apple-converted-space">    </span><span class="inbox-inbox-s1">func</span> testMethod() <span class="inbox-inbox-s1">throws</span> {</p>
<p class="inbox-inbox-p3"><span class="inbox-inbox-s3"><span class="inbox-inbox-Apple-converted-space">        </span></span><span class="inbox-inbox-s1">throw</span><span class="inbox-inbox-s3"> </span>MyError<span class="inbox-inbox-s3">.</span>justError</p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-Apple-converted-space">    </span>}</p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-Apple-converted-space">    </span><span class="inbox-inbox-s1">func</span> anotherMethod() {</p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-Apple-converted-space">        </span><span class="inbox-inbox-s1">do</span> {</p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-Apple-converted-space">            </span><span class="inbox-inbox-s1">try</span> <span class="inbox-inbox-s4">testMethod</span>()</p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-Apple-converted-space">        </span>} <span class="inbox-inbox-s1">catch</span> MyError.<span class="inbox-inbox-s4">justError</span> {</p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-Apple-converted-space">            </span><span class="inbox-inbox-s2">print</span>(<span class="inbox-inbox-s5">&quot;my error&quot;</span>)</p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-Apple-converted-space">        </span>} <b><span class="inbox-inbox-s1">catch</span> {</b></p>
<p class="inbox-inbox-p1"><b><span class="inbox-inbox-Apple-converted-space">            </span><span class="inbox-inbox-s2">print</span>(<span class="inbox-inbox-s5">&quot;other error&quot;</span>)</b></p>
<p class="inbox-inbox-p1"><b><span class="inbox-inbox-Apple-converted-space">        </span>}</b></p>
<p class="inbox-inbox-p1"><span class="inbox-inbox-Apple-converted-space">    </span>}</p>
<p class="inbox-inbox-p1">}</p><p class="inbox-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="inbox-inbox-p1">Please let me know if there is a way to do it and thanks for help in advance.<br></p><p class="inbox-inbox-p1">Tim Wang</p></div></div>