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

本文通过一个具体的示例详细介绍了Swift中的错误处理机制,包括如何使用do-try-catch语句来捕获并处理可能发生的错误,并特别强调了如何确保catch语句的完备性。
部署运行你感兴趣的模型镜像

在学习 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

}

您可能感兴趣的与本文相关的镜像

AutoGPT

AutoGPT

AI应用

AutoGPT于2023年3月30日由游戏公司Significant Gravitas Ltd.的创始人Toran Bruce Richards发布,AutoGPT是一个AI agent(智能体),也是开源的应用程序,结合了GPT-4和GPT-3.5技术,给定自然语言的目标,它将尝试通过将其分解成子任务,并在自动循环中使用互联网和其他工具来实现这一目标

X server non-fatal errors from xkbcomp typically occur during the X server startup process. These errors are related to the X Keyboard Extension compiler (`xkbcomp`), which is responsible for compiling and loading keyboard descriptions for the X server. One common cause of these errors is misconfiguration or missing files in the XKB configuration directory, usually located at `/usr/share/X11/xkb/`. If the X server cannot find or properly parse the keyboard description files, it may generate non-fatal errors, which are logged in the X server log file (typically found at `/var/log/Xorg.0.log`). These errors do not prevent the X server from starting but may result in unexpected keyboard behavior or the use of a default keyboard layout. To address these issues, you can take several steps. First, check the X server log file for specific error messages related to `xkbcomp`. These messages often provide clues about what is wrong with the configuration. For example, you might see messages like "Couldn't load XKB keymap," which indicates a problem with the keymap file. You can attempt to manually compile the keymap using `xkbcomp` to see if it provides more detailed error information. For instance, run the following command to compile the keymap: ```bash xkbcomp -w 1 -R/usr/share/X11/xkb -em1 -em2 -em3 -em4 -em5 -em6 -em7 -em8 -em9 -em10 -o /tmp/keymap.xkm $DISPLAY ``` This command tries to compile the current keymap and outputs it to a temporary file. If there are issues with the keymap, `xkbcomp` will report them, which can help in diagnosing the problem. If the problem is due to a missing or corrupted keymap file, you can try reinstalling the XKB configuration package. On Debian-based systems, you can use the following command: ```bash sudo apt-get install --reinstall xkb-data ``` For systems using RPM-based distributions, the command would be: ```bash sudo yum reinstall xkeyboard-config ``` After reinstalling the necessary packages, restarting the X server or rebooting the system may resolve the issue. It is also worth noting that if you are using a custom keyboard layout, ensure that it is correctly defined and that all necessary files are in the appropriate directories within the XKB configuration tree. Custom layouts should be placed in the `symbols` directory, and you may need to update the `evdev.xml` file to include your layout. In summary, non-fatal errors from `xkbcomp` related to the X server can be addressed by checking the X server logs for specific error messages, manually compiling the keymap to get more detailed error information, reinstalling the XKB configuration package if necessary, and ensuring that any custom keyboard layouts are correctly configured and placed in the appropriate directories[^1].
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值