title: Runtime error handling in Swift
body: <p>I am fully aware that Swift doesn't have a try/catch mechanism to catch exceptions (OK, Swift 2.0 now supports them). I also understand that many API methods return a NSError that will be filled with an error object if something goes wrong. So please don't point me to this question: <a href="https://stackoverflow.com/questions/24010569/error-handling-in-swift-language">Error-Handling in Swift-Language</a></p>
  
  <p>But this still doesn't explain how to react to runtime errors in your own code, like array-out-of-bounds accesses or force-unwrapping an optional value that is nil. For example:</p>
  
  <pre><code>var test: String?
  test = nil
  println(test!) //oops!
  </code></pre>
  
  <p>or</p>
  
  <pre><code>var arr = [0,1,2]
  for i = 0...3 {
      println(arr[i]) //oops!
  }
  </code></pre>
  
  <p>Every programmer makes such mistakes occasionally and there should be a way to at least log them for later analysis. While debugging, Xcode can show us those, but what if this happens to an end-user or beta-tester? In pure C there is signal handling and it could be used in Objective-C as well. Is there something like this in Swift? A centralized callback entered just before the app dies?</p>
  
  <p>Update:</p>
  
  <p>Let me rephrase the question: in a large project, it is not feasible to manually check for the above errors on every loop and force-unwrapping. When a runtime error does happen eventually, is there a callback like Objective C's segfault handling or NSSetUncaughtExceptionHandler that will get called so that the error can be logged/e-mailed together with a stacktrace of the crash?</p>