Search code examples
iosswiftwatchkit

Try Block throwing an error


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


Solution

  • 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")
                }
            }
        }