# TSPL Pitch: Typed throws

**URL:** <https://forums.swift.org/t/tspl-pitch-typed-throws/70526>\
**Category:** Swift Documentation Tooling\
**Created:** [March 8, 2024, 11:33pm UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526 "2024-03-08T23:33:38Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Martini](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alex_martini/32/1383_2.png) [@Alex\_Martini](https://forums.swift.org/u/Alex_Martini)\
**Post date:** [March 8, 2024, 11:33pm UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/1 "2024-03-08T23:33:39Z")

</div>

The majority of this update will be a new section in the Error Handling section of the guide — minor updates are needed throughout the reference and grammar. Here's the high-level outline:

- Contrast between `throws` and `throws(MyErrorType)` and `throws(any Error)`
- When to use typed throws
- The rules/behavior for inferring what type is thrown, and when you can let the compiler infer
- How to exhaustively handle typed errors in a `catch` block

For a detailed outline and the code example I plan to use, please see the diff linked below:

> **[Comparing apple:main...amartini51:typed\_throws\_118896654 · apple/swift-book](https://github.com/apple/swift-book/compare/main...amartini51:swift-book:typed_throws_118896654)**
>
> The Swift Programming Language book. Contribute to apple/swift-book development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [March 9, 2024, 9:30am UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/2 "2024-03-09T09:30:08Z")

</div>

Looking good, still excited to see this coming to the language A couple of points:

- Is it worth mentioning that a function that throws `Never` is equivalent to a non-throwing function? i.e `func f() throws(Never) -> T` is equivalent to `func f() -> T`. Perhaps an example of why this is useful, and how `rethrows` can be achieved with type throws (and is possibly preferred)?
- Is it worth labouring a bit more that regular throws is still preferred, and that package authors should still keep the surface area of their APIs as small as possible for the greatest flexibility. i.e. avoid encoding (nesting) the concrete errors of their dependencies in their own errors if possible. At one point or another, abuse of typed throws was a big concern:

> [@Precise error typing in Swift](https://forums.swift.org/t/precise-error-typing-in-swift/52045/1):
>
> I believe that Swift should move towards adopting precise error typing in order to solve fundamental expressivity problems with generics and allow the efficient returning of errors in low-level code. Unavoidably, some programmers will use that power in ways that I think are unwise, **but I think we can resist that tendency with community guidelines and pressure.**

Emphasis added. This was a while back now, so maybe in the light of day those concerns have softened somewhat.

---

<div class="post-metadata">

**Author:** ![Alex\_Martini](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alex_martini/32/1383_2.png) [@Alex\_Martini](https://forums.swift.org/u/Alex_Martini)\
**Post date:** [March 14, 2024, 9:11pm UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/3 "2024-03-14T21:11:12Z")

</div>

Thanks — yes, the docs need should point out specifically that `throws(Never)` is the same as not throwing.

By "laboring a bit more" were you suggesting that the new section in the guide should talk about this in more detail, or that it should be shorter to avoid belaboring the point?

---

<div class="post-metadata">

**Author:** ![tcldr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tcldr/32/24405_2.png) [@tcldr](https://forums.swift.org/u/tcldr)\
**Post date:** [March 14, 2024, 11:07pm UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/4 "2024-03-14T23:07:56Z")

</div>

> [@Alex\_Martini](#):
>
> By "laboring a bit more" were you suggesting that the new section in the guide should talk about this in more detail, or that it should be shorter to avoid belaboring the point?

I mean labouring in the positive sense: emphasising the point that regular untyped throws is still preferred and why.

---

<div class="post-metadata">

**Author:** ![James\_Dempsey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/james_dempsey/32/14336_2.png) [@James\_Dempsey](https://forums.swift.org/u/James_Dempsey)\
**Post date:** [March 15, 2024, 1:33am UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/5 "2024-03-15T01:33:12Z")

</div>

Hi @Alex_Martini,

I am providing this feedback as someone who is only familiar with the gist of typed throws from a high level.

First, overall, I found the outline clear and just reading the outline I feel I have a **much** better understanding of typed throws.

1. I strongly agree with @tcldr that it is important to emphasize that most code _does not_ need to use typed throws, and possibly spell out why using untyped throws is preferred.

2. In the list of reasons why you _would_ use typed throws, it wasn't clear to me why this would be one of those cases:

3. In the discussion of _"You can also use opaque types like `throws(some MyErrorProtocol)`"_, possibly clarify that you can't dynamically return different types of `MyErrorProtocol` at runtime. (Although I realize that is more of an opaque type topic than a typed throws topic.)

4. For me, the biggest unexpected syntax was seeing `throws` as part of `do` syntax:

5. Elsewhere in TSPL it is stated that ' Every `switch` statement must be _exhaustive_ .' and that includes switch statements that include a `default` case.

I think following that conceptual model, untyped throws can be handled exhaustively, if a general catch / default is provided, but it is only required by typed throws.

Definitely looking forward to the first fleshed-out draft!

---

<div class="post-metadata">

**Author:** ![wadetregaskis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wadetregaskis/32/8494_2.png) [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Post date:** [March 15, 2024, 4:47am UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/6 "2024-03-15T04:47:25Z")

</div>

> [@James\_Dempsey](#):
>
> In the list of reasons why you _would_ use typed throws, it wasn't clear to me why this would be one of those cases:
> 
> > - In code that only rethrows errors, especially when the throwing code comes from a closure the caller provided. Example: `map` in the stdlib. Xref to reference section -- this chapter doesn't discuss rethrows

That's good feedback. What it's trying to convey is that typed throws can be a partial replacement for `rethrows`, by codifying that the type(s) of errors thrown come from e.g. a closure argument (but it's even more powerful than that, because they can come from an associated type and other places too, unlike normal `rethrows`).

Unfortunately it can be a confusing subtopic because it's not a _superset_ of `rethrows`, _and_ typed throws are not currently fully compatible with `rethrows`.

---

<div class="post-metadata">

**Author:** ![James\_Dempsey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/james_dempsey/32/14336_2.png) [@James\_Dempsey](https://forums.swift.org/u/James_Dempsey)\
**Post date:** [March 15, 2024, 10:53pm UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/7 "2024-03-15T22:53:12Z")

</div>

> [@wadetregaskis](#):
>
> That's good feedback. What it's trying to convey is that typed throws can be a partial replacement for `rethrows`, by codifying that the type(s) of errors thrown come from e.g. a closure argument (but it's even more powerful than that, because they can come from an associated type and other places too, unlike normal `rethrows`).

Thank you for your explanation.

So, if I understand correctly, if I had a self-contained subsystem with its own set of errors implementing a protocol like `MySubsytemError`, it might be useful to write a method like this:

`func runsAClosure(_ closure: () throws(some MySubsytemError) -> String) throws(some MySubsytemError) -> String`

Where, instead of `rethrows` the function that takes a closure argument can report the type of the error it will throw?

But, the standard library `map()` _would not_ be able to use typed throws, since it already uses `rethrows`?

Is that at least in the ballpark of correct?

---

<div class="post-metadata">

**Author:** ![wadetregaskis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wadetregaskis/32/8494_2.png) [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Post date:** [March 16, 2024, 12:17am UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/8 "2024-03-16T00:17:13Z")

</div>

It should be:

```swift
func runsAClosure<E: MySubsystemError>(_ closure: () throws(E) -> String) throws(E) -> String

```

That way you know that the thrown error is _exactly_ the type that the closure throws, not merely some [potentially different] error that also conforms to `MySubsystemError`.

The difference with `rethrows` is that the above merely restricts the type of the [re]thrown error, it places no restrictions on _when_ an error can be thrown. `rethrows` actually enforces that `runsAClosure` can't throw unless the closure throws (sans some bugs and somewhat intentional escape hatches).

The two don't play well together yet because the combination simply isn't implemented. It's not clear if it ever will be. Some folks want to get rid of `rethrows` completely, rather than evolve it into the typed errors regime, even though that loses its unique functionality.

This is somewhat tangential to the discussion at hand (documenting typed throws), but the documentation probably does need to at least note the Venn-like relationship between typed throws and rethrows, and the current limitations of trying to combine them, and provide guidance on when to use one or the other.

---

<div class="post-metadata">

**Author:** ![James\_Dempsey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/james_dempsey/32/14336_2.png) [@James\_Dempsey](https://forums.swift.org/u/James_Dempsey)\
**Post date:** [March 16, 2024, 1:24am UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/9 "2024-03-16T01:24:59Z")

</div>

Thank you @wadetregaskis - yes that makes sense, thank you.

From the perspective of the documentation and that particular bullet point:

> - In code that only rethrows errors, especially when the throwing code comes from a closure the caller provided. Example: `map` in the stdlib. Xref to reference section -- this chapter doesn't discuss rethrows

For me, the mention of `map()` in the standard library as an example of this point was confusing.

I realize now, it isn't trying to say that `map()` can use typed throws, but that typed throws could be useful when writing a function that might otherwise use `rethrows`.

---

<div class="post-metadata">

**Author:** ![wadetregaskis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wadetregaskis/32/8494_2.png) [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Post date:** [March 16, 2024, 2:00am UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/10 "2024-03-16T02:00:39Z")

</div>

> [@James\_Dempsey](#):
>
> I realize now, it isn't trying to say that `map()` can use typed throws, but that typed throws could be useful when writing a function that might otherwise use `rethrows`.

Right. Though if `map` _does_ switch to typed throws, that ambiguity will be eliminated.

Though until then, it might be better for the documentation to pick a different example - one that does in fact use typed throws already.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [March 29, 2024, 12:41am UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/11 "2024-03-29T00:41:31Z")

</div>

> [@wadetregaskis](#):
>
> > [@James\_Dempsey](#):
> >
> > I realize now, it isn't trying to say that `map()` can use typed throws, but that typed throws could be useful when writing a function that might otherwise use `rethrows`.
> 
> Right. Though if `map` _does_ switch to typed throws, that ambiguity will be eliminated.
> 
> Though until then, it might be better for the documentation to pick a different example - one that does in fact use typed throws already.

The standard library _is_ adopting typed throws for `map`: this is part of the approved typed throws proposal.

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Ignatyev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmitriy_ignatyev/32/11340_2.png) [@Dmitriy\_Ignatyev](https://forums.swift.org/u/Dmitriy_Ignatyev)\
**Post date:** [March 29, 2024, 11:07am UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/12 "2024-03-29T11:07:44Z")

</div>

> [@James\_Dempsey](#):
>
> I strongly agree with @tcldr that it is important to emphasize that most code _does not_ need to use typed throws

While I agree with it in a whole, I'm also sure we need not only to claim "most code _does not_ need to use typed throws", "typed throws for those who understand what they are doing", "types throws are for special concrete needs" and so on. Documentation should provide real world examples, meaningful explanations, clear guidelines and more importantly when / why typed throws can cause tangible disadvantages.

Just one example from my current project: while we use type erased `any Error` / `some Error` across module boundaries in general, in module internals and more often in screen internals (privately) Typed `Result<T, ConcreteError>`s are used, specifically for Network Requests. Each request returns Result\<T: Codable, NetworkError\>. Network error is a enum with following cases:

- urlSessionError
- httpStatusError
- mappingError
- other (used only in a couple situations and is very uncommon).

As our product evolve very fast, it is a typical situation when product requirements are changed and we need to show different UI layout, user messages and suggest different actions to user for different http status codes. Worth to mention all of it is also often differs for urlSession / httpStatus / mapping errors.  
If we begin to use type erased errors by default and switch to concrete error types when they are really needed then we simply get a regression in time to market with no benefits to codebase. During migration process to modern concurrency these `Result`s will partly turn into typed throws.  
In different projects I saw a lot of clunky code where erased `any Error`s were casted to concrete types almost every time with pointless `else {}` / `default:` code branches for handling type casting failure which should never happen. Dynamic casting perfomance is not a problem at all for our project, but may be reasonable for others.

It just as with struct and classes where people should understand for which cases each one is preferred. But error handling in general is a less undestanble topic.

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Ignatyev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmitriy_ignatyev/32/11340_2.png) [@Dmitriy\_Ignatyev](https://forums.swift.org/u/Dmitriy_Ignatyev)\
**Post date:** [March 29, 2024, 12:00pm UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/13 "2024-03-29T12:00:23Z")

</div>

My own observation is that people tend to avoid using of difficult features like generics and very easily begin to use simple (by their opinion) features like new concurrency in a wrong way (e.g. spawning tons of `Task {}`, `Task.detached` {} and marking lots of non-UI code with `@MainActor`).  
I feel like something similar can happen with typed throws. I observe that developers often want to use concrete `Error` types for different reasons, but I would better say misconceptions:

- as users of strong-typed / statically-typed language, error types should also be strict (e.g. if we typically don't use `Array<any BinaryInteger>`, why should `Array<any Error` be preferred to `Array<ConcreteError>`)
- even we don't need concrete error type, it will be helpful if something in future will change
- don't need to type cast `any Error` and related burden
- impossibility or hard solving of a task if concrete error type is not visible in public namespace and therefore can't be typecasted, which involves raw integers and string literals comparisons with lots of `if-else`  
...

These are the common reasons for what I believe we should have clear explanations about typed throws.

Al in all, the feature itself is very attractive and amazing!

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Ignatyev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmitriy_ignatyev/32/11340_2.png) [@Dmitriy\_Ignatyev](https://forums.swift.org/u/Dmitriy_Ignatyev)\
**Post date:** [March 29, 2024, 12:24pm UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/14 "2024-03-29T12:24:27Z")

</div>

> [@Alex\_Martini](#):
>
> [Comparing apple:main...amartini51:typed\_throws\_118896654 · apple/swift-book · GitHub](https://github.com/apple/swift-book/compare/main...amartini51:swift-book:typed_throws_118896654)

**In code that has no dependencies, and only ever throws its own errors.**  
– own errors are also might not need to be exposed. A think `throws(some Error)` may have sense for both desktop and embedded swift.

```swift
extension NonEmptyOrderedDictionary {
  public init(from sequence: some Sequence) throws(some Error) {}
}

public struct HexColor: Hashable {
  ...
  public init(hexColorString: String) throws(some Error) { ... }
}

// Are there any reasons for typed throws here?

```

**TR: Does this include not depending on the stdlib?**  
Is there any way to not depend on stdlib?

---

<div class="post-metadata">

**Author:** ![Alex\_Martini](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alex_martini/32/1383_2.png) [@Alex\_Martini](https://forums.swift.org/u/Alex_Martini)\
**Post date:** [April 10, 2024, 10:48pm UTC](https://forums.swift.org/t/tspl-pitch-typed-throws/70526/15 "2024-04-10T22:48:56Z")

</div>

Thanks all for the feedback! I've made some [minor changes to the outline](https://github.com/amartini51/swift-book/commit/3ea9edd00dbe0ea3a7bf1c33e912b0b14896efc7) and I'll keep these comments in mind as I write the first draft.
