Swift:Errors thrown from here are not handled because the enclosing catch is not exhaustive
在學習 Swift 錯誤處理的時候,官方給出的 do-catch
例子如下:
...
...
let favoriteSnacks = [
"Alice": "Chips",
"Bob": "Licorice",
"Eve": "Pretzels",
]
func buyFavoriteSnack(person: String, vendingMachine: VendingMachine) throws {
let snackName = favoriteSnacks[person] ?? "Candy Bar"
try vendingMachine.vend(itemNamed: snackName)
}
var vendingMachine = VendingMachine()
vendingMachine.coinsDeposited = 8
do {
try buyFavoriteSnack(person: "Alice", vendingMachine: vendingMachine)
} catch VendingMachineError.invalidSelection {
print("Invalid Selection.")
} catch VendingMachineError.outOfStock {
print("Out of Stock.")
} catch VendingMachineError.insufficientFunds(let coinsNeeded) {
print("Insufficient funds. Please insert an additional \(coinsNeeded) coins.")
}
// Prints "Insufficient funds. Please insert an additional 2 coins."
但是親自上手敲程式碼的時候,卻總是在 "do" 閉包中 try
語句上報錯:
"Errors thrown from here are not handled because the enclosing catch is not exhaustive"
大體意思是說這個 do-catch
是不完整的。這時候需要再加上一個空的 catch
語句用於關閉 catch。
do {
try buyFavoriteSnack(person: "Alice", vendingMachine: vendingMachine)
} catch VendingMachineError.invalidSelection {
print("Invalid Selection.")
} catch VendingMachineError.outOfStock {
print("Out of Stock.")
} catch VendingMachineError.insufficientFunds(let coinsNeeded) {
print("Insufficient funds. Please insert an additional \(coinsNeeded) coins.")
} catch { // 加入一個空的catch,用於關閉catch。否則會報錯:Errors thrown from here are not handled because the enclosing catch is not exhaustive
}
相關文章
- Mysql host is blocked because of many connection errors;unblock解決方法MySqlBloCError
- [譯] 在 Swift 中使用 errors 作為控制流SwiftError
- Cannot load from short array because "sun.awt.FontConfiguration.head" is nullNull
- [20200416]ORA-01187 cannot read from file because it failed verification tests.AI
- Swift4 異常處理Try_Catch的使用之初見Swift
- [20181203]bash here $.txt
- 認識 Here Document
- Hello World! XJ is here.
- 【問題解決】java.sql.SQLException: null, message from server: “Host ‘xxx.xx.xx.xxx‘ is blocked because ofJavaSQLExceptionNullServerBloC
- try ,catch
- Working with Errors in Go 1.13ErrorGo
- Go Errors 詳解GoError
- slave-skip-errorsError
- Promise catch() 方法Promise
- Laravel try catchLaravel
- Canvas errors & CORS All In OneCanvasErrorCORS
- nvm command errors All In OneError
- React 18 errors All In OneReactError
- [20201109]here-doc(EOF) in bash.txt
- [20210218]xargs 與here doc測試.txt
- 手擼 Promise (then、catch)Promise
- Catch the Mole(Easy Version)
- catch_child.cpp
- “setting.xml” has syntax errorsXMLError
- POJ3278 Catch That Cow
- 忘不了的 TODOS & FIXMES & ERRORSError
- Django admin static files errors All In OneDjangoError
- [swift 進階]讀書筆記-第八章:錯誤處理 C8P8 錯誤鏈 Chaining ErrorsSwift筆記AIError
- Java try catch finally 總結Java
- JavaScript try catch finally 語句JavaScript
- JavaScript try/catch/finally 語句JavaScript
- try-catch-finally的使用
- Unity 解決 Because you are not a member of this projectUnityProject
- because it is a JDK dynamic proxy that implements 問題JDK
- From now on
- Unlucky because now anybody can shoot with their hot fix
- (十四).try-throw-catch機制
- try/catch 的解釋與用法
- js中try和catch的用法JS