0
votes

I am getting the following error message:

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

At the try statement below:

    if WCSession.isSupported() {
    let session = WCSession.defaultSession()
    if session.watchAppInstalled {
        let UserInfo = ["waste":floatWastedAmount]
        session.transferUserInfo(UserInfo)
        do {
            try session.updateApplicationContext(UserInfo)
        } catch let error as NSError {
            NSLog("Updating the context failed: " + error.localizedDescription)
        }
    }
}

I am not sure why it says non-exhaustive as any error should execute the NSLog statement. Any pointers would help

2
Which Xcode version are you using? Your code should compile (and it does in my Xcode 7.0.1), as all errors can be converted to NSError. - Martin R
Version 7.1 beta 3 (7B85) ... Just tried it in 7.0.1, get the same error.. and failed compile. - Michael Rowe

2 Answers

0
votes

Change to the following.. for general catch of all errors:

 if WCSession.isSupported() {
        if session.watchAppInstalled {
            let UserInfo = ["waste":floatWastedAmount]
            do {
                try session.updateApplicationContext(UserInfo)
            } catch {
                print("Updating the context failed")
            }
        }
    }
0
votes

The list of caught errors must be exhaustive meaning, you have to handle every possible case. Just like switch statements and just like it you can use an empty catch as a break. so your code is fine, just add empty catch

do {
    try session.updateApplicationContext(UserInfo)
} catch let error as NSError {
    NSLog("Updating the context failed: " + error.localizedDescription)
} catch {}