Learning Swift from scratch (Day54)-throwing errors

Source: Internet
Author: User

Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog

A function or method that can be placed behind a try is required, and they are likely to throw an error, which is appended with the throws keyword after the arguments to the function or method declaration, indicating that the function or method can throw an error.

The example code for declaring a throw error method is as follows:

// Delete Note Recording method func Remove (model:note) throws {  ...} // querying all record data methods  -- [Note] {    ...}

The above code remove (_:) method has no return value, and the throws keyword is placed after the parameter. FindAll () has a return value of throws keyword placed between the parameter and the return value type.

Throw an error in a function or method

A function or method can declare a throw error because an error is generated and thrown in a function or method, so that a function or method declaration throws an error in practical sense.

In generating and throwing the wrong way:

L artificially throws an error in a function or method through a throw statement.

Other functions or methods that are called in a function or method can throw an error function or method, but no catch processing can cause errors to be propagated.

The sample code is as follows:

//Delete Note Methodfunc Remove (model:note)throws{Guard Let date= Model.dateElse{//when the judgment is thrown, there is a guard statement.//throw "PRIMARY key is empty" error        ThrowDaoerror.primarykeynull}//Compare date primary keys are equal     for(index, note) in Listdata.enumerate () where note.date = =Date {listdata.removeatindex (index)}}//querying all data methodsfunc findAll ()throws-[Note] {guard Listdata.count> 0Else{//when the judgment is thrown, there is a guard statement.//throws "no data" error.         ThrowDaoerror.nodata}returnlistdata}func printnotes ()throws{//declaration throws an errorLet datas=TryfindAll () forNote in datas {print ("Date: \ (note.date!) -Content: \ (note.content!) ")    }}TryPrintNotes ()

The Guard statement is best at dealing with this early judgment, which throws an error when the condition is false.

The FINDALL () statement itself is likely to produce an error, but it is not captured and processed using a catch statement, which causes the error to propagate to the caller of the function or method, and if neither of its callers captures processing, the last program will have a run-time error.

Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
?
More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com
Luxgen Classroom Forum Website: http://51work6.com/forum.php

Learning Swift from scratch (Day54)-throwing errors

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.