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.