迅速-如何处理未捕获的异常 [英] swift - How to deal with uncaught exception

查看:119
本文介绍了迅速-如何处理未捕获的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If use NSSetUncaughtExceptionHandler, it only handles objective-C runtime errors. 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])

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

How swift deal with non-objective-C runtime errors(swift runtime errors)??

推荐答案

如果您要处理索引异常,则可以始终验证索引项

If you are looking to handle out of index exception then you can always verify the index item

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
    }
}

这篇关于迅速-如何处理未捕获的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆