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

The error handling mode of the Swift 1.x has many drawbacks, for example: in order to save time in programming, passing a nil to the error parameter, or not judging whether the error is nil after the method call is complete, and not handling error.

Let contents = nsstring (contentsoffile:filepath,        êencoding:nsutf8stringencoding, Error:nil)// the error parameter passes a nil

Or

var err:nserror? = NSString (contentsoffile:filepath,        &err)    

Bad programming habits, because Objective-c and Swift 1.x do not have a mandatory processing mechanism, so once a real error occurs, the program will crash.

The same example of reading a string from a file, if using the Swift 2 error handling mode code is as follows:

Import do {        // to do some action      try  nsstring ( Contentsoffile:filepath,                             êencoding:nsutf8stringencoding)    // what to try to do   Catch// if failed, enter catch code block     Err.Description}

Do-try-catch This error pattern is very similar to the exception handling mechanism in Java, which is intended to be an attempt to do one thing and catch (catch) processing if it fails.

Catch error

The syntax for the complete Do-try-catch error handling pattern is as follows:

 Do {    trycatch  match error {    error-handling statement group}

An error can be generated in a try statement, and of course it may not produce an error, and if an error occurs, the catch handles 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 swift, the error type must conform to the ErrorType protocol, followed by the match of the error type, which 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 that there is no data condition, and Primarykeynull indicates that the table's primary key (Primary key) is empty.

Then we can catch the error with the following code.

 Do {     //try to access the data table function or method  catch  daoerror.nodata {    print (" No data. "catch  daoerror.primarykeynull {    print (" The 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
?
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

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.