Learn Swift from scratch Learning Note (day)--do-try-catch error handling mode

Source: Internet
Author: User

original articles, welcome reprint. Reprint Please specify: Dongsheng's blog

Swift 1.x error-handling mode, for example: in order to save time in programming, give Error parameter to pass a Nil , or when the method call is complete, do not judge Error whether to Nil , no error handling is performed.

Let contents =nsstring (Contentsoffile:filepath, encoding:nsutf8stringencoding, Error:nil)//error parameters pass a nil or Var    Err:nserror? Let contents =nsstring (Contentsoffile:filepath, encoding:nsutf8stringencoding, error: &err)


bad programming habits, because objective-c and the Swift 1.x There is no enforcement mechanism, so the program crashes once a real error occurs.

the same example of reading a string from a file, if you use the Swift2 The error handling mode code is as follows:

Import Foundation do {//To do some operations let str = try NSString (Contentsoffile:filepath, ENC oding:nsutf8stringencoding)//What to try to do} catch let err as nserror{//if failed enter catch code block Err.Description}


Do-try-catch This error mode is associated with Java the exception handling mechanism is very similar, the intention is to try ( Try ) To do one thing, if the failure is captured ( Catch ) processing.  

Catch error

Full- Do-try-catch the syntax for error handling mode is as follows:

Do {Try statement successfully processed statement group} catch match error {error handling statement group}


in the Try The statement can produce an error, of course, it may not produce an error, if an error occurs, Catch will handle the error. A catch code block can have multiple, and the error is determined by which catch code block is handled by the error after the catch . The number of error types determines How many catch can be. Let's introduce the error type first.  

Type of error

in the Swift the type of error must follow ErrorType protocol, followed by the matching of the error type, it should be designed as an enumeration type, which is ideal for associating a set of related values.

If we write access to the database table program and implement operations such as inserting, deleting, modifying, and querying the table data, we will need an error type similar to the following code:

Enum Daoerror:errortype {case NoData case Primarykeynull}


NoData indicates no data condition, Primarykeynull represents the primary key of the table ( Primary Key ) is an empty case.

Then we can catch the error with the following code.

Do {//try accesses a data table function or method} catch Daoerror.nodata {print ("no data. ")} catchdaoerror.primarykeynull {print (" PRIMARY key is empty. ")}


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
650) this.width=650; "title=" 00.png "src=" http://s4.51cto.com/wyfs02/M01/7C/EF/wKioL1bc8pHTDcPdAAAs2MBEZnc177.png "alt=" Wkiol1bc8phtdcpdaaas2mbeznc177.png "/>
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php


This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1748300

Learn Swift from scratch Learning Note (day)--do-try-catch error handling mode

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.