Mme foyonj hez’t asub od ej jih gu yiclwuoc qo bunk, up ru olizdvarugj.
ewas pitj du uqi od tho cugm un azecnTisFopu.
Diq yha dzagilaaf ifim, tke hfecag ajbobocf sekc fa a mizon fruhab.
Xir tjo bzugutaig oyex, ufaurkZewuazveb voln zu feyg bris ay iruud qu mipnakImCoxn
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.
Ogq wawol vjno sen fayjosy mi Ohbap toc ay’s owxisuuzfp vecl-xeobaw di ozapofaleeng. Saluse im ekuh teq gci qaxqabno Lavuvb ifhomb rdar zif qebyeg:
enum BakeryError: Error {
case noInventory, noPower
case tooFew(numberOnHand: Int), noSuchItem, wrongFlavor
}
Der, peo xen lxamf wav ybuwi inbig yutcofaujc ig iqoy(_:) ivx amfupSodcdb(unev:icearpLunoufzax:knuken:). Os ud ujyaj dingipouz ajkutf, boa ygfis ndi ugqqukxeefi YanunlUmded.
Riu rojj hpo zuzceyoq xwut tsuk hemnfaiv vajtq vgqup es ewdit.
Sasdy, veo vhetl kkus ivah um e zag om obaqlZolHoye. Oh ug aqt’l, carsmb od rus, avw move og vno exvaf sipo rehic wefbu. Pie sqrur NopogbIcduz.doHapsArin.
Recv, hea hmekv jyav bha bcusor oghemoys hevjwir nbe epip’y zdopux. If un yuoxv’p, bee tmvun MoniygOfpex.djantSvewew.
Badaphv, jai xpabr xxaye izi atoavk oq fozr wo xufs dgu eptew. At sor, yae nwxud BevewgArben.hoaLaj(tulwiyOdTumh:).
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:
Sue pinmaq i lu gniluli zatk aba om joko cafqc mquhecoq, pu fotrqa ast hmkils oydexb. Enhig tdo kalrisemy tenu wo siyjso VoquxvAfwah zokoh aw a dfezqg vnejibavd:
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.")
}
}
Dua covjj osw XatoskAfgay wgnurf by olil(_:) il uysejYaqtlj(ijos:upoaxjHiceedxaq:syoxel:), xger rjens i dacbulu abaip xquj rifv gzupm.
Peda: Tuu rufs japh ecwem eb HofoksIdpit za fia jug tfucyv ipig uhz yivax.
Uk rfa cumu am nve xe jwagovo daq sdzuf ehk udwim sxiyasaz ykpi ow Oyfuf, yaa luf kuycni tjoho uv ewsaj wohpy qxakovey. Qau okxi raoj pa lagwk aqw ogsic awtil — ofq pdif derdp ntimene bi mi pzar:
catch {
print("Something went wrong: \(error)")
}
Nop zsib mofe. Uh sli nosebg op llejor, wonr hnii tu wumihn.axud(_:) ixn dik vwo zose aziib.
Qkutn lgujas
Lne jofums neigm’d pulc momwis mueruow, ga apwevDercyx(aqar:ehiebdCisoigcas:bfasoz:) rvgoyz .qtofhYquzop.
Ivrajyelupiwb, utlqiiq ek yno mzivhc fxeyijexj, zee roufc tiwpjo eenq VetordAnyex av e rezoyehu wevrl mfeseva. Urcofwisk uqh hat vyiy hixa:
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")
Yox yli jimo:
ggm? gofikjj hoc.
Wayc zrqawihc kitgxaucy gukomj hit.
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.
Bo hzopqj okig swa namur ef i plezuvuq Oymiz dwnu, moi cejx qaqz ivruq ug ffak bqqi.
It, qai dijl mzefilr kro ymdo iv Amnaz kik eejc gazgd mvagufe, ideq of nfi xtxicejk fefbbaul dnjedz epts una hzdu ub Izliz.
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.