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

Inj*_*joy 10 exception swift

如果使用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])
Run Code Online (Sandbox Code Playgroud)

NSSetUncaughtExceptionHandler无法捕捉异常:

var a = [""]
println(a[2])
Run Code Online (Sandbox Code Playgroud)

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

Iva*_*nti -3

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

  • 我的意思是未捕获的异常处理程序,而不是“try catch” (2认同)