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 errorsMySqlBloCError
- 【Mysql】MySql Host is blocked because of many connection errors;MySqlBloCError
- How to recover from USB pipe errorsError
- Oracle DB Hanging Issues for versions from 7 to 9--Exhaustive_61552.1Oracle
- tar: Error exit delayed from previous errorsError
- Mysql host is blocked because of many connection errors;unblock解決方法MySqlBloCError
- Mysql連線錯誤:Mysql Host is blocked because of many connection errorsMySqlBloCError
- [譯] 在 Swift 中使用 errors 作為控制流SwiftError
- SAP Errors described – from 61-228 to 61-245Error
- SAP Errors described – from 61-21 to 61-227Error
- 從原始碼解析ERROR 1129 (HY000):Host is blocked because of many connection errors原始碼ErrorBloC
- Swift4 異常處理Try_Catch的使用之初見Swift
- 【譯】Swift2 中的錯誤處理:try,catch,do 以及 throwSwift
- Oracle開發誤區(here)Oracle
- Right here waitingAI
- No enclosing instance of type Outer is accessible.
- Laravel try catchLaravel
- Promise catch() 方法Promise
- js try catchJS
- 利用HERE Document攜帶C原始碼原始碼
- Metric Collection ErrorsError
- oracle之errorsOracleError
- oracle 之errorsOracleError
- 手擼 Promise (then、catch)Promise
- iOS的@try、@catch()iOS
- [20200416]ORA-01187 cannot read from file because it failed verification tests.AI
- Leetcode - Smallest Rectangle Enclosing Black PixelsLeetCode
- 《從零開始學Swift》學習筆記(Day 53)——do-try-catch錯誤處理模式Swift筆記模式
- Go Errors 詳解GoError
- slave-skip-errorsError
- HIbernate errors...Error
- Java try catch finallyJava
- 【問題解決】java.sql.SQLException: null, message from server: “Host ‘xxx.xx.xx.xxx‘ is blocked because ofJavaSQLExceptionNullServerBloC
- Python Enclosing作用域、裝飾器話聊上篇Python
- LeetCode-Smallest Rectangle Enclosing Black PixelsLeetCode
- WPF捕獲事件即使這個事件被標記為Handled事件
- Working with Errors in Go 1.13ErrorGo
- Errors and Recovery for the Server Parameter FileErrorServer