首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >swift -如何处理未捕获的异常

swift -如何处理未捕获的异常
EN

Stack Overflow用户
提问于 2015-06-24 19:01:50
回答 2查看 1.8K关注 0票数 11

如果使用NSSetUncaughtExceptionHandler,它只处理objective-C运行时错误。https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/index.html#//apple_ref/c/func/NSSetUncaughtExceptionHandler

NSSetUncaughtExceptionHandler可以捕获异常:

代码语言:javascript
运行
复制
var a: NSArray = [""]
println(a[2])

但是NSSetUncaughtExceptionHandler不能捕获异常:

代码语言:javascript
运行
复制
var a = [""]
println(a[2])

如何快速处理非objective-C运行时错误( swift运行时错误)?

EN

回答 2

Stack Overflow用户

发布于 2019-08-05 19:06:26

如果您希望处理超出索引的异常,则始终可以验证索引项

代码语言:javascript
运行
复制
extension Collection {
    /// Returns the element at the specified index if it is within bounds, otherwise nil.
    public subscript (safe index: Index) -> Element? {
      return indices.contains(index) ? self[index] : nil
    }
}
票数 0
EN

Stack Overflow用户

发布于 2015-06-24 21:43:16

已经有一个类似的问题Error-Handling in Swift-Language得到了详尽的回答。请看第一个答案,其中包括Swift 2.0中有关此主题的最新更新。

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31024803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档