- iOS Programming Cookbook
- Hossam Ghareeb
- 248字
- 2021-07-09 18:29:45
How it works...
We started our code example by creating a new error type called SignUpUserError, which conforms to ErrorType protocol. As we see, we listed four errors that may happen while signing up any user in our system, such as invalid first name or last name, invalid e-mail, weak password, and passwords that don't match. So far, so good!
Then, we create a function signUpNewUserWithFirstName, which takes user input values, and as we can see, we have marked it with the throws keyword. The keyword throws says that this function may throw an error anytime during execution, so you be prepared to catch errors thrown by this method.
Inside the implementation of the function, you will see a list of guard statements that checks for user input; if any of these guard statements returned false, the code of else statement will be called. The statement throw is used to stop execution of this method and throw the appropriate error based on the checking made.
Catching errors is pretty easy; to call a function that throws error, you have to call it inside the do-catch block. After the do statement, use the try keyword and call your function. If any error happens while executing your method, the block of code inside the catch statement will be called with a given parameter called error that represents the error. We've created a switch statement that checks the type of error and prints a user-friendly statement based on the error type.
- Linux設(shè)備驅(qū)動(dòng)開發(fā)詳解(第2版)
- Linux內(nèi)核完全注釋(20周年版·第2版)
- 嵌入式Linux系統(tǒng)開發(fā):基于Yocto Project
- 操作系統(tǒng)基礎(chǔ)與實(shí)踐:基于openEuler平臺(tái)
- Installing and Configuring Windows 10:70-698 Exam Guide
- 完美應(yīng)用RHEL 8
- 操作系統(tǒng)分析
- AWS SysOps Cookbook
- Introduction to R for Quantitative Finance
- Windows 10從新手到高手
- 從實(shí)踐中學(xué)習(xí)Windows滲透測(cè)試
- 從零開始學(xué)安裝與重裝系統(tǒng)
- Web Penetration Testing with Kali Linux(Third Edition)
- Linux系統(tǒng)安全:縱深防御、安全掃描與入侵檢測(cè)
- Microsoft Hyper-V Cluster Design