Swift Study Notes (2): handle errors and swift Study Notes
Directory:
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!