# \[PITCH\] Add Ability To Specify Error Type On Throws

**URL:** <https://forums.swift.org/t/pitch-add-ability-to-specify-error-type-on-throws/3910>\
**Category:** Pitches\
**Created:** [August 30, 2016, 10:54pm UTC](https://forums.swift.org/t/pitch-add-ability-to-specify-error-type-on-throws/3910 "2016-08-30T22:54:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Robinson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mark_robinson/32/17050_2.png) [@Mark\_Robinson](https://forums.swift.org/u/Mark_Robinson)\
**Post date:** [August 30, 2016, 10:54pm UTC](https://forums.swift.org/t/pitch-add-ability-to-specify-error-type-on-throws/3910/1 "2016-08-30T22:54:33Z")

</div>

Hello,

Consider the scenario of the vending machine example in the Swift Book:

We have a function that throws errors from a single given ErrorType

&nbsp;&nbsp;&nbsp;1. func vend(itemNamed name: String) throws {  
&nbsp;&nbsp;&nbsp;2. guard let item = inventory[name] else {  
&nbsp;&nbsp;&nbsp;3. throw VendingMachineError.InvalidSelection  
&nbsp;&nbsp;&nbsp;4. }  
&nbsp;&nbsp;&nbsp;5.  
&nbsp;&nbsp;&nbsp;6. guard item.count \> 0 else {  
&nbsp;&nbsp;&nbsp;7. throw VendingMachineError.OutOfStock  
&nbsp;&nbsp;&nbsp;8. }  
&nbsp;&nbsp;&nbsp;9.  
&nbsp;&nbsp;&nbsp;10. guard item.price \<= coinsDeposited else {  
&nbsp;&nbsp;&nbsp;11. throw VendingMachineError.InsufficientFunds(coinsNeeded: item.price  
&nbsp;&nbsp;&nbsp;- coinsDeposited)  
&nbsp;&nbsp;&nbsp;12. }

In this scenario - when we have a function that throws a single type of  
error - I believe it would be useful to be able to \*optionally\* specify the  
type of error thrown from the vend function, so that the code would look  
like this:

&nbsp;&nbsp;&nbsp;1. func vend(itemNamed name: String) throws VendingMachineError {  
&nbsp;&nbsp;&nbsp;2. guard let item = inventory[name] else {  
&nbsp;&nbsp;&nbsp;3. throw .InvalidSelection  
&nbsp;&nbsp;&nbsp;4. }  
&nbsp;&nbsp;&nbsp;5.  
&nbsp;&nbsp;&nbsp;6. guard item.count \> 0 else {  
&nbsp;&nbsp;&nbsp;7. throw .OutOfStock  
&nbsp;&nbsp;&nbsp;8. }  
&nbsp;&nbsp;&nbsp;9.  
&nbsp;&nbsp;&nbsp;10. guard item.price \<= coinsDeposited else {  
&nbsp;&nbsp;&nbsp;11. throw .InsufficientFunds(coinsNeeded: item.price - coinsDeposited)  
&nbsp;&nbsp;&nbsp;12. }

The purpose is to just let the compiler know that all errors thrown from  
this function will be of type VendingMachineError so there's no longer any  
need to constantly prefix the enumeration cases with the enumeration type.

This makes the code much cleaner to read and stops repetition of  
VendingMachineError.  
In the scenario where there's multiple types of errors thrown from a  
function, using throws as we do now without a type would suffice, as in  
this case it makes sense that the programmer has to tell the compiler what  
type the Error is via prefixing the enumeration case with the enumeration  
type.

Cheers,  
Mark

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@Chris\_Lattner](https://forums.swift.org/u/Chris_Lattner)\
**Post date:** [August 31, 2016, 7:59pm UTC](https://forums.swift.org/t/pitch-add-ability-to-specify-error-type-on-throws/3910/2 "2016-08-31T19:59:01Z")

</div>

Please catch up on the thread earlier this week on this exact topic.

-Chris

> **···**
>
> > On Aug 30, 2016, at 3:54 PM, Mark Robinson via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > Hello,
> > 
> > Consider the scenario of the vending machine example in the Swift Book:
