Again, you specify throws(BakeryError) and delete BakeryError from each BakeryError case.
And, when catching errors thrown by these two methods, remove explicit mentions of BakeryError:
do {
try bakery.open()
try bakery.orderPastry(item: "Cookie", amountRequested: 1, flavor: "ChocolateChip")
}
// Handle each BakeryError
catch let error { // 1
switch error {
case .inventory, .noPower: // 2
print("Sorry, the bakery is now closed.")
case .doNotSell:
print("Sorry, but we don't sell this item.")
case .wrongFlavor:
print("Sorry, but we don't carry this flavor.")
case .tooFew(numberOnHand: let items):
print("We only have \(items) cookies left.")
}
}
//catch { // 3
// print("Something went wrong: \(error)")
//}
You don’t need to cast error as BakeryError — option-click it to see it’s already of type BakeryError.
You don’t need to specify BakeryError for its cases.
If you don’t delete this catch closure, the compiler warns “Case will never be executed”.
Backwards Compatibility
You can still use throws without specifying an Error type — the compiler translates it to throws(any Error). And, it converts functions that don’t throw an error to throws(Never).
Ac i feim-nahe uyxmoxuheox, yjuw ifsloakv hiefx qaor ba uq uncwajiuv ux Ekdej dmvin wi swiq udceq Iyham mjzeg, stasf puenf otx ad os i jilrdut ralk uy Iqjot vgxuv. Adc vau’x zwomx jare ri qe u buw iv xjesdqifc ixr rdutlusx si suhnli abogk olkef. Udgudfodm de Bogmujn Logs Hpotl, “cju eovxevt av bdu ofoveqeam jwumeved [daan cvij] odik wolw bca upbuhaec ad jdkep hjkikr pa Hrorj, iqkrzun xhsumx ab qehyac jic dunp ytuvahoap”.
See forum comments
This content was released on Sep 5 2025. The official support period is 6-months
from this date.
Investigate how typed throws can help the compiler help you.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.