How do I get the error message in Swift 2.0?
Solution 1
Use automatic error
variable, and you can cast it to NSError
if you wish:
catch {
let nsError = error as NSError
print(nsError.localizedDescription)
}
Solution 2
You can now throw any object inheriting ErrorType
, and provide custom handling in the catch
sentence. You can also cast the error to NSError
to access localizedDescription
for handling third party errors.
Casting an enum ErrorType
will produce a NSError
with domain
equal to the enum name, code
equal to the enum value and an auto-generated localizedDescription
with the following format:
The operation couldn’t be completed. (DOMAIN error CODE.)
For example, the following code:
enum AwfulError: ErrorType {
case Bad
case Worse
case Terrible
}
func throwingFunction() throws {
throw AwfulError.Worse
}
do {
try throwingFunction()
}
catch AwfulError.Bad {
print("Bad error")
}
catch let error as NSError {
print(error.localizedDescription)
}
Will print
The operation couldn’t be completed. (AwfulError error 1.)
Solution 3
Despite the question title specifying Swift 2, this answer is for Swift 3.
As @redent84 points out, since Swift 2 an Error object may be a home-made one. Here's a method I wrote to analyze and print the default error object available in a "catch" statement that doesn't specify any specific error type:
// Method to print an unknown Error type object to the system output.
static func printCaughtError(_ unknownError : Error) {
let objectDescription = String(describing: unknownError)
let localizedDescription = unknownError.localizedDescription
if localizedDescription != "" {
if localizedDescription.contains(objectDescription) {
print(localizedDescription)
return
}
if !objectDescription.contains(localizedDescription) {
print(objectDescription + ": " + localizedDescription)
return
}
}
print(objectDescription)
}
Then you can call it like this:
catch {
printCaughtError(error)
}
Arbitur
Been working as a Mobile App Developer for iOS and Android since 2014. Loves problem solving and learning new things. 100% self taught so I really like what I do, its my job and part of my hobbies. I recently (as of 2018) started with 3DPrinting and also have years of CAD experience.
Updated on June 13, 2022Comments
-
Arbitur almost 2 years
I used this method very much in Swift 1.2:
NSURLConnection.sendSynchronousRequest(:_:_:_)
but this is apparently deprecated in iOS9. It still works however but now it uses the new Swift 2.0 Error Handling and I don't know how I will get the error message if it fails, ex. if time runs out.I know I have to put it into a do-catch and then say try before the metho but I dont know how to catch the error message.
do { let data = try NSURLConnection.sendSynchronousRequest(request, returningResponse: nil) return data } catch _ { return nil }
Before I used NSError and then its description property, but now I have no clue.
-
Martin R over 8 years@redent84: Any ErrorType is bridged to NSError automatically, that is documented in the "Using Swift with Cocoa and Objective-C" book.
-
Martin R over 8 yearsOr simply
catch let error as NSError { ...}
as in (for example) stackoverflow.com/questions/30954722/…. -
Arbitur over 8 yearsThanks I didnt see any info that the error is passed like that, I only found several catches that took a specific error or none at all. I can accept in 3 min
-
rshev over 8 yearsYou'll get some standard
localizedDescription
for Swift-onlyErrorType
. -
Oleksii Nezhyborets over 7 yearsThere is default
localizedDescription
for anyError
now (see link). But from my tests it seems that it simply doeslet nsError = error as NSError
for us. developer.apple.com/reference/swift/error/….