@@ -709,40 +709,42 @@ you don't know ahead of time every error that could happen
709
709
while the code is running,
710
710
especially when propagating errors thrown somewhere else.
711
711
This approach also reflects the fact that errors can change over time.
712
- New versions of a library can throw new errors ---
713
- including libraries used by your dependencies ---
712
+ New versions of a library ---
713
+ including libraries that your dependencies use ---
714
+ can throw new errors,
714
715
and the rich complexity of real-world user configurations
715
716
can expose failure modes that weren't visible during development or testing.
716
717
The error handling code in the examples above
717
718
always includes a default case to handle errors
718
- that don't have their own specific ` catch ` clause.
719
+ that don't have a specific ` catch ` clause.
719
720
720
721
Most Swift code doesn't specify the type for the errors it throws.
721
722
However,
722
- in some special cases,
723
- you might limit code to throwing errors of only one specific type :
723
+ you might limit code to throwing errors of only one specific type
724
+ in the following special cases :
724
725
725
726
- When running code on an embedded system
726
727
that doesn't support dynamic allocation of memory.
727
- Throwing an instance ` any Error ` or another boxed protocol type
728
+ Throwing an instance of ` any Error ` or another boxed protocol type
728
729
requires allocating memory at runtime to store the error.
729
- Throwing an error of a specific type
730
- lets Swift allocate that memory upfront instead.
730
+ In contrast,
731
+ throwing an error of a specific type
732
+ lets Swift allocate that memory upfront.
731
733
732
- - When the errors are used only within some unit of code,
734
+ - When the errors are an implementation detail of some unit of code,
733
735
like a library,
734
736
and aren't part of the interface to that code.
735
- Because the errors come only from the library,
737
+ Because the errors come from only the library,
736
738
and not from other dependencies or the library's clients,
737
739
you can make an exhaustive list of all possible failures.
738
740
And because these errors are an implementation detail of the library,
739
- and they're always handled within that library.
741
+ they're always handled within that library.
740
742
741
- - In code that only throws errors that were thrown elsewhere,
743
+ - In code that throws only errors that were thrown elsewhere,
742
744
like a function that takes a closure argument
743
745
and propagates any errors from that closure.
744
- For a comparison between ` rethrows `
745
- and throwing a specific, generic, error type
746
+ For a comparison between propagating a specific error type
747
+ and using ` rethrows ` ,
746
748
see < doc:Declarations:Rethrowing-Functions-and-Methods > .
747
749
748
750
For example,
@@ -756,9 +758,9 @@ enum StatisticsError: Error {
756
758
}
757
759
```
758
760
759
- To specify that a function throws only ` StatisticsError ` values as its errors
760
- you write ` throws(StatisticsError) ` when declaring the function,
761
- instead of just writing ` throws ` .
761
+ To specify that a function throws only ` StatisticsError ` values as its errors,
762
+ you write ` throws(StatisticsError) ` instead of only ` throws `
763
+ when declaring the function .
762
764
This syntax is also called * typed throws*
763
765
because you write the error type after ` throws ` --- for example:
764
766
@@ -783,7 +785,7 @@ This function throws an instance of `StatisticsError` if the input isn't valid.
783
785
Both places in the code above that throw an error
784
786
omit the type of the error
785
787
because the function's error type is already defined.
786
- You can use the short form like ` throw .noRatings `
788
+ You can use the short form, ` throw .noRatings ` ,
787
789
instead of writing ` throw StatisticsError.noRatings `
788
790
when throwing an error in a function like this.
789
791
@@ -845,10 +847,10 @@ In this code,
845
847
writing ` do throws(StatisticsError) ` indicates that
846
848
the ` do ` -` catch ` statement throws ` StatisticsError ` values as its errors.
847
849
Like other ` do ` -` catch ` statements,
848
- the ` catch ` clause can either handle every possible error,
849
- or it can propagate unhandled errors for some surrounding scope to handle.
850
- Here, it handles all of the errors,
851
- using a switch with one case for each enumeration value.
850
+ the ` catch ` clause can either handle every possible error
851
+ or propagate unhandled errors for some surrounding scope to handle.
852
+ This code handles all of the errors,
853
+ using a ` switch ` statement with one case for each enumeration value.
852
854
Like other ` catch ` clauses that don't have a pattern,
853
855
the clause matches any error
854
856
and binds the error to a local constant named ` error ` .
@@ -857,17 +859,17 @@ Because the `do`-`catch` statement throws `StatisticsError` values,
857
859
858
860
<!-- XXX show multiple catch clauses with different patterns? -->
859
861
860
- The ` catch ` clause above uses a switch
862
+ The ` catch ` clause above uses a ` switch ` statement
861
863
to match and handle each possible error.
862
864
If you tried to add a new case to ` StatisticsError `
863
865
without updating the error-handling code,
864
866
Swift would give you an error
865
- because the switch wouldn't be exhaustive anymore.
867
+ because the ` switch ` statement wouldn't be exhaustive anymore.
866
868
For a library that catches all of its own errors,
867
869
you could use this approach to ensure any new errors
868
870
get corresponding new code to handle them.
869
871
870
- If a function or ` do ` block throws only errors of a single type,
872
+ If a function or ` do ` block throws errors of only a single type,
871
873
Swift infers that this code is using typed throws.
872
874
Using this shorter syntax,
873
875
you could write the ` do ` -` catch ` example above as follows:
889
891
890
892
Even though the ` do ` -` catch ` block above
891
893
doesn't specify what type of error it throws,
892
- it's still understood as throwing ` StatisticsError ` .
894
+ Swift infers that it throws ` StatisticsError ` .
893
895
You can explicitly write ` throws(any Error) `
894
896
to avoid letting Swift infer typed throws.
895
897
0 commit comments