swift-using try? and try!

Source: Internet
Author: User



When you use try for error handling, you often see a question mark (?) or exclamation point (!) behind the try, what's the difference between them?



1. Use try?



Try? The error is converted to an optional value, and when the try?+ function or method statement is called, if the function or method throws an error, the program does not crash and returns a nil, and an optional value is returned if no error is thrown.



The sample code is as follows:


[HTML]View PlainCopyprint?


Querying all data methods
Func FindAll () throws-> [Note] {
Guard Listdata.count > 0 Else {
Throws "No data" error.
Throw Daoerror.nodata
}
Return ListData
}
Let datas = try? FindAll ()
Print (datas)










In the above code, try is used in the Let datas = try? FindAll () statement , anddatas is an optional value, in this case, nil. It is not necessary to use the do-catch statement to wrap the try statement.  



2. Use Try !



Using try! can break the chain of error propagation. When the error is thrown, it is propagated to its callers, which forms a chain of propagation, but sometimes does not want to let the error propagate, you can use the try! statement.



Modify the above code as follows:





[HTML]View PlainCopyprint?





Querying all data methods
Func FindAll () throws-> [Note] {
Guard Listdata.count > 0 Else {
Throws "No data" error.
Throw Daoerror.nodata
}
Return ListData
}
Func printnotes () {
Let datas = try! findAll () ①
For note in Datas {
Print ("Date: \ (note.date!) -Content: \ (note.content!) ")
}
}
PrintNotes () ②







The Code printnotes () function does not declare a throw error and does not require the TRY keyword when it is called , and the error propagation chain is broken within the printnotes () function.



The code changes the Try Dao.findall () statement to try! FindAll () and adds an exclamation point (!) after the try so that thecompiler does not require the PrintNotes () method declaration to throw an error. Try! breaks the chain of error propagation, but if there is a real error, a run-time error causes the program to crash.



So when using try! to break the chain of error propagation, you should ensure that the program does not error.



swift-using try? and try!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.