swift--【do..catch与try,try?,try!】

throws an exception is thrown, then it must be handled by a try

try: the standard treatment, which must be integrated to handle do catch

try:? tell the system may be wrong, but also probably true, if an error occurs, it returns nil, if no error occurred,
met data packaged into an optional value returned to us this type of use, equivalent to ignore errors

try:! tell the system must be right, if an error occurs, the program will crash is not recommended.

// 1. Obtain JSON file path
let path = NSBundle.mainBundle () pathForResource ( "MainVCSettings.json", ofType: nil).!

// 2. Load JSON files
let data = NSData (contentsOfFile: path )!

// 3. JSON incoming data file is loaded into an object dictionary array
// do..catch used in conjunction with the try
do {
// write possible error code
let objc = try NSJSONSerialization.JSONObjectWithData (data, options: NSJSONReadingOptions .MutableContainers)
  } catch {
// do long curly braces code throws an exception, executes catch
// braces if the latter do not throw an exception, the catch back braces that a code execution
WFLog ( error)
}

Guess you like

Origin www.cnblogs.com/sanjianghuiliu/p/11234123.html
try