swift - 在 Swift 中抛出多个异常

标签 swift exception error-handling

一个函数怎么会抛出多个异常? 例如在 Java 中是这样的:

private Int f(Int data, boolean e)
        throws AException, BException,
        CException {...

谢谢

最佳答案

该语言不支持它,很可能是因为它被认为是一种反模式。但是,只要有点聪明才智,您就可以得到一个包含错误数组的错误案例:

enum MyError: Error {
    case general
    case notFound
    case invalid
    case multiple([MyError])
}

func test() throws {
    throw MyError.multiple([.general, .invalid])
}

关于swift - 在 Swift 中抛出多个异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43138740/

相关文章:

ios - 如何在 Firebase iOS 上进行浅查询?

c# - Entity Framework 的重复键异常?

java - 应用发布后如何处理RuntimeException

go - errors.Wrapf()、errors.Errorf() 和 fmt.Errorf() 之间有什么区别?

快速调试打印与打印

ios - swift:推迟非可选对象初始化

ios - Swift:for循环中的异步调用

java - 线程 "Thread-0"java.lang.NoClassDefFoundError : Server$WorkerThread 中出现异常

php - PHP 7 CLI : is there any method to skip undefined functions?

angular - IONIC 4 ionic 菜单打开关闭时出现菜单错误