Slide 1

Slide 1 text

૪ ࠴ ࡻ ࡨ ࡥ ࢗ ࢊ ࠴ ࡪ ࢫ ߭

Slide 2

Slide 2 text

Slide 3

Slide 3 text

cockscomb

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

ࡻ ࡨ ࡥ ࢗ ࢊ

Slide 6

Slide 6 text

Swift 2

Slide 7

Slide 7 text

ࡪ ࢫ ߭

Slide 8

Slide 8 text

Error Handling

Slide 9

Slide 9 text

• Error Handling Basics • guard, defer • Closure • Asynchronous callback • Exhaustive

Slide 10

Slide 10 text

Error Handling Basics

Slide 11

Slide 11 text

enum MyError: Int { static let domain = "MyErrorDomain" case DangerousError = -999 } func awesome(error: NSErrorPointer) -> String? { error.memory = NSError(domain: MyError.domain, code: MyError.DangerousError.rawValue, userInfo: nil) return nil } func test() { var error: NSError? let result = awesome(&error) if let r = result { print(r) } if let e = error { print(e) } } test() Swift 1.x

Slide 12

Slide 12 text

enum MyError: ErrorType { case DangerousError } func awesome() throws -> String { if fail { throw MyError.DangerousError } return "awesome" } func test() { do { let result = try awesome() print(result) } catch { print("Error") } } test() Using ErrorType

Slide 13

Slide 13 text

enum MyError: ErrorType { case DangerousError(String) } func awesome() throws -> String { if fail { throw MyError.DangerousError("Too dangerous") } return "awesome" } func test() { do { let result = try awesome() print(result) } catch MyError.DangerousError(let message) { print(message) } catch { print("Error") } } test() Error information

Slide 14

Slide 14 text

enum MyError: ErrorType { case DangerousError } func awesome() throws -> String { if fail { throw MyError.DangerousError } return "awesome" } func test() throws { let result = try awesome() print(result) } do { try test() } catch { print("Error") } Propagate error

Slide 15

Slide 15 text

enum MyError: ErrorType { case DangerousError } func awesome() throws -> String { if fail { throw MyError.DangerousError } return "awesome" } func test() { let result = try! awesome() print(result) } test() Forced-try

Slide 16

Slide 16 text

Error Handling Basics • Before Swift 2, NSErrorPointer • ErrorType • throw, try, catch • Propagation

Slide 17

Slide 17 text

guard, defer

Slide 18

Slide 18 text

struct Dog { enum Error: ErrorType { case Sleeping } enum State { case Sitting case Sleeping case Walking } var state: State = .Sitting mutating func walk() throws { guard state != .Sleeping else { throw Error.Sleeping } state = .Walking } } guard

Slide 19

Slide 19 text

struct Human { enum State { case Standing case Walking } var state: State = .Standing mutating func walk(inout dog: Dog) throws { defer { dog.praise() } try dog.walk() state = .Walking } } defer

Slide 20

Slide 20 text

Closure

Slide 21

Slide 21 text

enum MyError: ErrorType { case DangerousError } func execute(closure: () throws -> Void) throws { try closure() } do { try execute({ throw MyError.DangerousError }) } catch { print("Error") } Closures can throw

Slide 22

Slide 22 text

enum MyError: ErrorType { case DangerousError } func execute(closure: () throws -> Void) throws { try closure() } do { try execute({ print("No error") }) } catch { print("Error") } Assumed no error but catch

Slide 23

Slide 23 text

enum MyError: ErrorType { case DangerousError } func execute(closure: () throws -> Void) rethrows { try closure() } execute({ print("No error") }) Assumed no error

Slide 24

Slide 24 text

Closure • try closure • rethrows

Slide 25

Slide 25 text

Asynchronous callback

Slide 26

Slide 26 text

enum MyError: ErrorType { case DangerousError } func doAsync(callback: (result: String?, error: ErrorType?) -> Void) { dispatch_async(dispatch_get_main_queue()) { callback(result: nil, error: MyError.DangerousError) } } doAsync { (result, error) in if let result = result { print(result) } else if let error = error { print("Error") } } Asynchronous callback

Slide 27

Slide 27 text

enum MyError: ErrorType { case DangerousError } func doAsync(callback: (() throws -> String) -> Void) { dispatch_async(dispatch_get_main_queue()) { callback({ throw MyError.DangerousError }) } } doAsync { (f) in do { let result = try f() } catch { print("Error") } } Asynchronous callback

Slide 28

Slide 28 text

enum ValueOrError { case Value(T) case Error(ErrorType) init(_ value: T) { self = Value(value) } init(_ error: ErrorType) { self = Error(error) } func get() throws -> T { switch self { case .Value(let value): return value case .Error(let error): throw error } } } Value or Error

Slide 29

Slide 29 text

enum MyError: ErrorType { case DangerousError } func doAsync(callback: (ValueOrError) -> Void) { dispatch_async(dispatch_get_main_queue()) { callback(ValueOrError(MyError.DangerousError)) } } doAsync { (v) in do { let result = try v.get() } catch { print("Error") } } Asynchronous callback

Slide 30

Slide 30 text

Asynchronous callback • Optional, Optional • f: () throws -> Result • ValueOrError value

Slide 31

Slide 31 text

func doAsync(callback : String catches -> Void) { if (success) { callback("hooray!") } else { callback(throw MyError.Failed) } } doAsync { result in // handle result } catch { // Catch async thrown error } Suggested in developer forums https://forums.developer.apple.com/thread/4354 Unreal

Slide 32

Slide 32 text

Exhaustive

Slide 33

Slide 33 text

–The Swift Programming Language “Like a switch statement, the compiler attempts to infer whether catch clauses are exhaustive. If such a determination can be made, the error is considered handled.”

Slide 34

Slide 34 text

enum MyError: ErrorType { case DangerousError } func doSomething() throws { throw MyError.DangerousError } func test() { do { try doSomething() } catch MyError.DangerousError { print("Error") } }

Slide 35

Slide 35 text

Errors thrown from here are not handled b ! enum MyError: ErrorType { case DangerousError } func doSomething() throws { throw MyError.DangerousError } func test() { do { try doSomething() } catch MyError.DangerousError { print("Error") } } Not exhaustive

Slide 36

Slide 36 text

Errors thrown from here are not handled because the enclosing catch is not exhaustive. !

Slide 37

Slide 37 text

Errors thrown from here are not handled b ! enum MyError: ErrorType { case DangerousError } func doSomething() throws { throw MyError.DangerousError } func test() { do { try doSomething() } catch MyError.DangerousError { print("Error") } } Not exhaustive

Slide 38

Slide 38 text

• Error Handling Basics • guard, defer • Closure • Asynchronous callback • Exhaustive

Slide 39

Slide 39 text

૪ ࠴ ࡻ ࡨ ࡥ ࢗ ࢊ ࠴ ࡪ ࢫ ߭