try try? try! (轉自stackoverflow)

http://stackoverflow.com/questions/32390611/try-try-try-what-s-the-difference-and-when-to-use-each

歪果仁解釋的非常清楚

Assume the following throwing function:

enum ThrowableError : ErrorType { case BadError }

func doSomething() throws -> String {
    if everythingIsFine {
        return "Everything is ok"
    } else {
        throw ThrowableError.BadError
    }
}
try

You have 2 options when you try calling a function that may throw.

You can take responsibility of handling errors by surrounding your call within a do-catch block:

do {
    let result = try doSomething()
}
catch {
    // Here you know about the error
    // Feel free to handle to re-throw
} 

Or just try calling the function, and pass the error along to the next caller in the call chain:

func doSomeOtherThing() throws -> Void {    
    // Not within a do-catch block.
    // Any errors will be re-thrown to callers.
    let result = try doSomething()
}
try!

What happens when you try to access an implicitly unwrapped optional with a nil inside it? Yes, true, the app will CRASH! Same goes with try! it basically ignores the error chain, and declares a “do or die” situation. If the called function didn’t throw any errors, everything goes fine. But if it failed and threw an error, your application will simply crash.

let result = try! doSomething() // if an error was thrown, CRASH!
try?

A new keyword that was introduced in Xcode 7 beta 6. It returns an optional that unwraps successful values, and catches error by returning nil.

if let result = try? doSomething() {
    // doSomething succeeded, and result is unwrapped.
} else {
    // Ouch, doSomething() threw an error.
}

Or we can use new awesome guard keyword:

guard let result = try? doSomething() else {
    // Ouch, doSomething() threw an error.
}
// doSomething succeeded, and result is unwrapped.

One final note here, by using try? note that you’re discarding the error that took place, as it’s translated to a nil. Use try? when you’re focusing more on successes and failure, not on why things failed.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末冶伞,一起剝皮案震驚了整個濱河市新症,隨后出現的幾起案子,更是在濱河造成了極大的恐慌响禽,老刑警劉巖徒爹,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異芋类,居然都是意外死亡隆嗅,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門侯繁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來胖喳,“玉大人,你說我怎么就攤上這事贮竟±龊福” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵咕别,是天一觀的道長技健。 經常有香客問我,道長惰拱,這世上最難降的妖魔是什么雌贱? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上欣孤,老公的妹妹穿的比我還像新娘馋没。我一直安慰自己,他們只是感情好降传,可當我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布披泪。 她就那樣靜靜地躺著,像睡著了一般搬瑰。 火紅的嫁衣襯著肌膚如雪款票。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天泽论,我揣著相機與錄音艾少,去河邊找鬼。 笑死翼悴,一個胖子當著我的面吹牛缚够,可吹牛的內容都是我干的。 我是一名探鬼主播鹦赎,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼谍椅,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了古话?” 一聲冷哼從身側響起雏吭,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎陪踩,沒想到半個月后杖们,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡肩狂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年摘完,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片傻谁。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡孝治,死狀恐怖,靈堂內的尸體忽然破棺而出审磁,到底是詐尸還是另有隱情谈飒,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布力图,位于F島的核電站步绸,受9級特大地震影響,放射性物質發(fā)生泄漏吃媒。R本人自食惡果不足惜瓤介,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一吕喘、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧刑桑,春花似錦氯质、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至琢锋,卻和暖如春辕漂,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背吴超。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工钉嘹, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人鲸阻。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓跋涣,卻偏偏與公主長得像,于是被迫代替她去往敵國和親鸟悴。 傳聞我的和親對象是個殘疾皇子陈辱,可洞房花燭夜當晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內容