如果使用 NSSetUncaughtExceptionHandler
,它只处理 objective-C 运行时错误。
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/index.html#//apple_ref/c/func/NSSetUncaughtExceptionHandler
NSSetUncaughtExceptionHandler
可以捕获异常:
var a: NSArray = [""]
println(a[2])
但是NSSetUncaughtExceptionHandler
无法捕获异常:
var a = [""]
println(a[2])
How swift deal with non-objective-C runtime errors(swift runtime errors)??
最佳答案
如果您希望处理超出索引的异常,那么您始终可以验证索引项
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
}
}
关于swift - 如何处理未捕获的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31024803/