20

I have an iOS application written in Swift 2 in Xcode 8.2.1, that's built for iOS 10.2.

I've had a number of crash reports from TestFlight and despite symbolication, none of the crash logs show any program state besides the stack-traces (no argument values, no locals, no heap objects, etc).

...but inside those functions I can see code which is likely to fail (e.g. a forced unwrap) but the crash log isn't telling me where or why it's failing.

When debugging in Xcode, I can use fatalError(message: String) where I can put my own message like "functionFoo returned nil" or "variable bar == \"" + bar + "\"", except when deployed using TestFlight or the App Store the fatalError will be hit and the program terminates, but the message value is not saved to the crash log, making it pointless.

In other environments, like C#/.NET and Java I can simply throw new SomeExceptionType("my message") and all information is available in whatever global catch(Exception) handler I have.

How can I achieve the same goal in iOS / Swift?

Dai
  • 141,631
  • 28
  • 261
  • 374

1 Answers1

-1

Swift does support error handling. You can create your own error type by confirming to Error protocol or use existing error types and then throw an error by invoking throw error.

But Swift forces you add error handling to any code that can throw an error. There are multiple way you can handle error in swift.

  1. Apply throws keyword to your function, this indicates that the function can throw an error when invoked and the error should be handled by the caller.

    func canThrowErrors() throws -> String
    
  2. When invoking methods with throws keyword you have to add try keyword at the beginning of the invocation. All these try invocations should be handled either by applying throws to method to just propagate the errors or wrapping inside a do-catch block:

    do {
        try canThrowErrors()
        try canThrowOtherErrors()
    } catch is SpecificError {
        // handling only specific error type
    } catch let error as SpecificError {
        // catches only specific error for type
    } catch {
        // catches all errors
    }
    
  3. Additionally you can use try? and try! for throwing function invocation to disable error propagation and retrieve optional result that returns nil in case of error and runtime assertions respectively.

By forcing you to handle all the errors at compile time swift avoids any undefined runtime behavior and debugging nightmare.

I would suggest to use fatalError or any other runtime assertion only if scenarios when there is no way to recover from a state without crashing the app. Unfortunately, there is no way to handle errors from fatalError as its use is only reserved for such scenarios only. Also, in your crashlog you will only get the line number that caused the crash to get additional info for the cause of crash I would suggest to use custom logging or analytics.

Soumya Mahunt
  • 2,148
  • 12
  • 30