Swift 4:
According to:
https://developer.apple.com/documentation/foundation/nserror
if you don't want to define a custom exception, you can use the standard NSError object as follows:
import Foundation do { throw NSError(domain: "my error description", code: 42, userInfo: ["ui1":12, "ui2":"val2"] ) } catch let error as NSError { print("Caught NSError: \(error.localizedDescription), \(error.domain), \(error.code)") let uis = error.userInfo print("\tUser info:") for (key,value) in uis { print("\t\tkey=\(key), value=\(value)") } }
Print:
Caught NSError: The operation could not be completed, my error description, 42 User info: key=ui1, value=12 key=ui2, value=val2
This allows you to provide a custom string, as well as a numeric code and a dictionary with all the necessary additional data of any type.
Note: this has been tested on OS = Linux (Ubuntu 16.04 LTS).
PJ_Finnegan Oct 06 '17 at 16:40 2017-10-06 16:40
source share