美文网首页
try try? try! (转自stackoverflow)

try try? try! (转自stackoverflow)

作者: 童冀 | 来源:发表于2016-08-23 10:58 被阅读46次

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.

相关文章

  • try try? try! (转自stackoverflow)

    http://stackoverflow.com/questions/32390611/try-try-try-w...

  • GCD相关基础知识

    原创文章,转载请注明:转自:Try_Try_Try 1.获取Dispatch Queue 的两种方法 方法一: 通...

  • KVC调用规则

    原创文章,转载请注明:转自:Try_Try_Try 背景 关于KVC的使用方法,网络上已有很多文章介绍,这篇文章讲...

  • try try? try!

    try try? try! 也是好晕 如这样的情况 报错提示 需要加上 try, try是swift 2...

  • try  try  try

    2017年1月9日练了一节艾扬格小班课程,力量和精准练习,让我从胳膊酸到腿,身体累,但心情却很放松,夜里也深沉的睡...

  • try、try?、try!

    try: try与do-catch语句一起使用,并对错误进行详细处理。 try? try?用在错误无关紧要的时候,...

  • try try! try? And Realm

    使用Realm的时候需要取一个Realm的实例,官方示例如下: // Get the default Realm ...

  • Get up and try try try

    迷迷糊糊的状态已经有两个月了 没有方向的乱转着 这是一个低谷和瓶颈 让我不知道怎么继续走下去 每天有家的陪伴 心却...

  • 1-3碎碎念

    ①"try to do" vs "try doing" Try andis often used instead ...

  • try and try hard

    If you're going to try, go all the way. Otherwise, don't ...

网友评论

      本文标题:try try? try! (转自stackoverflow)

      本文链接:https://www.haomeiwen.com/subject/zwogsttx.html