Error-Handling in Swift-Language -
i haven't read much swift 1 thing noticed there no exceptions. how error handling in swift? has found error-handling?
runtime errors:
as leandros suggests handling runtime errors (like network connectivity problems, parsing data, opening file, etc) should utilize nserror did in objc, because foundation, appkit, uikit, etc study errors in way. it's more framework thing language thing.
another frequent pattern beingness used separator success/failure blocks in afnetworking:
var sessionmanager = afhttpsessionmanager(baseurl: nsurl(string: "yavin4.yavin.planets")) sessionmanager.head("/api/destorydeathstar", parameters: xwingsquad, success: { (nsurlsessiondatatask) -> void in println("success") }, failure:{ (nsurlsessiondatatask, nserror) -> void in println("failure") }) still failure block received nserror instance, describing error.
programmer errors:
for programmer errors (like out of bounds access of array element, invalid arguments passed function call, etc) used exceptions in objc. swift language not seem have language back upwards exceptions (like throw, catch, etc keyword). however, documentation suggests running on same runtime objc, , hence still able throw nsexceptions this:
nsexception(name: "somename", reason: "somereason", userinfo: nil).raise() you cannot grab them in pure swift, although may opt catching exceptions in objc code.
the questions whether should throw exceptions programmer errors, or rather utilize assertions apple suggests in language guide.
swift 2.0 update
things have changed bit in swift 2.0, there new error-handling mechanism, more similar exceptions different in detail.
indicating error possibilityif function/method wants indicate may throw error, should contain throws keyword
func ridethedragon() throws { ... } note: there no specification of function can throw, either throwing error or not throwing @ all.
invoking function may throw errorsin order invoke function need utilize seek keyword, this
try ridethedragon(dragon: dragon) this line should nowadays do-catch block this
do { seek ridethedragon(reddragon) } grab dragonerror.dragonismissing { // error-handling } grab dragonerror.notenoughmana(let manarequired) { // more error-handlng } grab { // grab error-handling } note: grab clause utilize powerful features of swift pattern matching flexible here.
alternatively in function marked throws keyword this
func fullfillquest(quest: quest) throws { seek ridethedragon(quest.dragon) } last not least, can decide know error not occur, e.g. because have checked prerequisites, , utilize try! keyword.
try! ridethedragon(goldendragon) if function throws error, you'll runtime error in application.
throwing errorin order throw error utilize throw keyword this
throw dragonerror.dragonismissing you can throw conforms errortype protocol. starters nserror conforms protocol go enum-based errortype enables grouping multiple related errors, potentially additional pieces of data, this
enum dragonerror: errortype { case dragonismissing case notenoughmana(manarequired: int) ... } main differences between new swift 2.0 error mechanism , java/c#/c++ style exceptions follows:
syntax bit different:do-catch + try + defer vs traditional try-catch-finally syntax. exception handling incurs much higher execution time in exception path in success path. not case swift 2.0 errors, success path , error path cost same. all error throwing code must declared, while exceptions might have been thrown anywhere. errors "checked exceptions" in java nomenclature. however, in contrast java, not specify potentially thrown errors. swift exceptions not compatible objc exceptions. do-catch block not grab nsexception, , vice versa, must utilize objc. swift exceptions compatible cocoa nserror method conventions of returning either false (for bool returning functions) or nil (for anyobject returning functions) , passing nserrorpointer error details. as syntatic-sugar ease error handling, there 2 more concepts
deferred actions (usingdefer keyword) allow accomplish same effect blocks in java/c#/etc guard statement (using guard keyword) allow write little less if/else code in normal error checking/signaling code. swift
No comments:
Post a Comment