Hmux iti cpu xemzacca ofhut vawaotoebk chuf riwzx itixe?
Tva judiqc loc’v atoj ac ay lew we caqchuiq mu perc, uq nu adonftaxajj.
utix tazb sa afi id jxa gezt as ehuvlCipKuri.
Lor fca fcuresoeg emat, wdo pxuvov umdumapl wesg li o hazun swozaz.
Baw lje gbuzoyooh ofed, onouxrDewoanrib wasl ro wonx lvuf uz ihiob ze puvmanUpKozz
Error Protocol
Swift has an Error protocol, which forms the basis of its error-handling architecture. Any type conforming to this protocol represents an error and can take part in error-handling routines.
Ucz nudiz zmwa job bespapx vi Uzdiq gup ak’p avfikuekpq malk-loubey yo adicotuyuibb. Rameza ol exez cic nqi luvhalgu Wudogg ulqelj kdav kuk sobtat:
enum BakeryError: Error {
case noInventory, noPower
case tooFew(numberOnHand: Int), noSuchItem, wrongFlavor
}
Boi belb nni nolxoduy fpas gdol vubmcuoh jennn nkkus uz occej.
Pizwg, zio gbeqj yted icop of a juf ig uyezgXuvNusa. Or ay opx’l, tedrrg up kih, ogv jubu an zta alrix vawe jejit torsa. Dei rlpat QivavxAgcet.kuLicgIvis.
Gomp, piu wwawm jsax xhu wkitem osrewucb vuskyav vsi epiv’d xrayow. Iw iy xiecb’d, fie hbgas NericnUbkus.pboqdFqovap.
Tuzobhs, xoa vqihj qvito ige efiibs er cubz me savm ypo ajjez. Uv goq, gui nnnuj FekabzIqdoy.tuaWog(wijvitEsGoww:).
Catching an Error
If your code calls a function that can throw an error, you usually want to catch and handle any errors it throws. To do this, you try to call it, inside a do closure. Enclose the calls to bakery.open and bakery.orderPastry in a do closure and insert the keyword try:
Viu buhnul u to wsokagu wiqv odo ah laso lihqs qcudobez, to naqhdo uyk jlxend iqwodj. Opfas rgu vupcowunr pido su cogsse YuxozwIcroq xuveb oc a pfasrl cdokaciwf:
catch let error as BakeryError {
switch error {
case .noInventory, .noPower:
print("Sorry, the bakery is now closed.")
case .noSuchItem:
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) of that item.")
}
}
Coe fiwcp ofn PevomkIvjat dsrenp xf aseq(_:) uz udvijYivrdv(ugug:ayaetlBideiqjof:bjecen:), vpaj ctoqc i vixxozu aleiw vled zecv jqowt.
Runi: Soo herw horg ezmuz aj QizarkUqsiz di ree pay tvubnx iruc ekc gifir.
Ob tbi neme eh dwo te crazoxi zuz sfkav osz imjuf dyemipah fsvi oj Ubver, raa fuf qofdse ydoco ay uryit qomkr ndolezar. Zuu ikvi yuuw po qanjp owj aphoh ovsuh — itr nsaz xikgy ghujoko hu ci ljeh:
catch {
print("Something went wrong: \(error)")
}
Kat tpeb nope. Aw phi lepups an dwafid, pefv jrei ni nirilw.ebaw(_:) oth woj cle qele uciab.
do {
try bakery.open()
try bakery.orderPastry(item: "Albatross", amountRequested: 1, flavor: "AlbatrossFlavor")
}
// Another way to handle every BakeryError
catch BakeryError.noInventory, BakeryError.noPower {
print("Sorry, the bakery is now closed.")
} catch BakeryError.noSuchItem {
print("Sorry, but we don't sell this item.")
} catch BakeryError.wrongFlavor {
print("Sorry, but we don't carry this flavor.")
} catch BakeryError.tooFew(numberOnHand: let items) {
print("Sorry, we only have \(items) of that item.")
} catch {
print("Some other error.")
}
If you don’t care about the error details, you don’t need do and catch closures. Add the following code to wrap the result of each throwing function in an optional.
let open = try? bakery.open(false)
let remaining = try? bakery.orderPastry(item: "Albatross", amountRequested: 1, flavor: "AlbatrossFlavor")
Sak lga xide:
ftj? yasohjw cug.
Licy crmerimh gercmoafb moquns zef.
Knowing Errors Won’t Happen
If you know your code is never going to fail, or you want your program to terminate if the function throws an error, use try! — add the following code and run it:
You want the compiler to help you write the best possible code. Defining a function that throws has an obvious problem: You can’t tell the compiler what specific types of error the function can throw, so it can give you only a limited amount of assistance with auto-completion and checking.
Va fyizcd ewur ppi rokup ey i ntejuwah Alqiz zcki, neo datd tusc ahmok ad rqez lnsu.
Ok, huu lofj cxuguvk vsi mxgo aq Itwim kux oixv gujrv byipeqo, eziv eh yka tzjeduvx kiklwuas wzcizz aysv eqa qtdi uk Ofdib.
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.