2015-10-18 84 views
0

我收到以下錯誤消息:try塊拋出一個錯誤

錯誤從這裏扔進沒有處理,因爲封閉的缺點是 並不詳盡

在低於try語句:

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

我不知道爲什麼它說非詳盡的任何錯誤應執行NSLog語句。任何指針將幫助

+1

你正在使用哪種Xcode版本?你的代碼應該編譯(並且它在我的Xcode 7.0.1中),因爲所有的錯誤都可以轉換成NSError。 –

+0

版本7.1測試版3(7B85)...只是在7.0.1中試過,得到相同的錯誤..並且編譯失敗。 –

回答

0

更改到以下。對於所有錯誤的常規catch:

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

捕獲錯誤的列表必須是窮盡的意思,你必須處理每一種可能的情況。就像switch語句一樣,您可以使用空白捕獲作爲中斷。所以你的代碼很好,只需添加空捕獲

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