THE 用于在 Swift 中进行 Exceptional Programming
的框架
throws
)-> Never {
)函数看起来像这样
func name(...) throws -> Never {
...
}
import XPKit
dependencies: [
.package(url: "https://github.com/ExceptionalProgramming/XP-Swift",
from: "1.0.0")
],
targets: [
.target(
name: "",
dependencies: [
"XPKit"
]),
]
异常是一个应该被 thrown
的 class
(类)
Exception (异常)
ProgramTerminated (程序终止异常)
RuntimeException (运行时异常)
FatalException (致命异常)
ValueException (值异常)
所有 do/catch
代码块都应该重新抛出任何未处理的异常
...
} catch let error { throw error }
do {
try ...
} catch _ as ProgramTerminated {
} catch let error { throw error }
我们理解并非所有代码都是异常的,因此我们提供了 get<T>(from:) -> T
函数,它将从抛出的 ValueException<T>
中提取值
提供了一个 add<T: Addable>
函数,它 throws
所有值的总和。Int
和 UInt
类型族都遵循 Addable
协议