11

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 can catch the exception:

var a: NSArray = [""]
println(a[2])

But NSSetUncaughtExceptionHandler can not catch exceptions:

var a = [""]
println(a[2])

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

Injoy
  • 174
  • 1
  • 11

2 Answers2

0

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
    }
}
Nick
  • 1,127
  • 2
  • 15
  • 40
-3

There has already been an exhaustively answered similar question Error-Handling in Swift-Language. Look at the first answer there which includes the latest updates in Swift 2.0 regarding this topic.

Community
  • 1
  • 1
Ivan Rigamonti
  • 696
  • 5
  • 7
  • 2
    I mean the Uncaught Exception Handlers, rather than `try catch` – Injoy Jun 25 '15 at 02:26
  • @Injoy - have you got the solution – Durai Amuthan.H Feb 23 '16 at 13:30
  • This does not answer the question. The linked question is about user-level errors, this question is about catching and handling fatal errors. My research on the topic indicates that there currently is no equivalent way to catch fatal errors. – Fizker Mar 23 '16 at 16:38