Handle exceptions

 Problem

I want to handle possible exceptions/errors, using a Do-Catch statement.



 Recipe


// Let's try reading from a file

do {
    let contents = try NSString(contentsOfFile: "myfile.txt", encoding: NSUTF8StringEncoding)
    print(contents)
}
catch let error as NSError {
    // Catch a possible error
    
    print("Ooops! Something went wrong: \(error)")
}
				

Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)

 

 Playground