Swift Study Notes (2): handle errors and swift Study Notes

Source: Internet
Author: User

Swift Study Notes (2): handle errors and swift Study Notes

Directory:

  • Do-catch
  • Assertions
Do-catch

In Swift, throws can be used to throw exceptions during program execution, and do... catch can be used externally to catch exceptions thrown by methods limited by try.

Func makeASandwich () throws {// This function may throw an error} do {try makeASandwich () eatASandwich ()} catch SandwichError. outOfCleanDishes {washDishes ()} catch SandwichError. missingIngredients (let ingredients) {buyGroceries (ingredients)} catch {// normal Exception Handling}

 

Assertions

Use the assert ([conditional expression]) Global Method in the program to determine whether the return value of the expression is true to determine whether to terminate the program.

Let age =-3 assert (age> = 0, "A person's age cannot be less than zero") // triggers an asserted

 

Disclaimer: this series of content is from the Internet or e-books. Only learning summaries are supported!

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.