Have you ever tried logging into an app just to get an error message that was not successful and wondered why? Or have you ever filled an online form and tried submitting the form just to receive a message that some required fields were not filled in yet. All these scenarios are errors that happened within an application. And one of the ways it can be reported in code is by using ThrowableFunctions. In this video, you'll discover what ThrowableFunctions are as well as learn how to write your own. Consider the scenario. Little Lemon offers a rewards program for its regular customers. Customers can register for the rewards program on the Little Lemon app, where they can customize their profiles and subscribe to receive newsletters. If you think about implementing a user-registration feature, how will your code handle reporting user information that is missing or incomplete? For instance, when a user provides a malformed email or tries to use a password that the app considers is not secure, as a developer, you need to prepare your code to handle such cases. But how? Well, with the help of ThrowableFunctions, you can structure your code to report unexpected issues in a concise and reusable manner. ThrowableFunctions are functions that throw an error when certain conditions are not met, for instance, if the user password isn't strong enough. Not all functions are throwable by default, only when the keyword throws is added immediately after the function definition will a function be throwable. For example, the String class in swift has a constructor that loads the contents of a file using a path argument. This function will initialize a string with the full content of the file at the given path. The throws keyword following the function definition indicates that the code expects certain problems to occur while trying to load the file. For example, if the file can't be found in the path specified, declaring a function is throwable is just one part of it. The second part is throwing an error inside the function itself when certain conditions are not met. To make any custom type, an enumeration, or class throwable, it must conform to an error protocol. While the error protocol has no requirements of its own, you can inherit the protocol to represent an error in Swift's error handling system. To throw an error, you can use the keyword throw, followed by any type that inherits an error protocol. For example, NSError is an error class provided by Swift that conforms to an error protocol, but it's not always handy to use general NSError class provided by the system. In some cases, it's more convenient to define custom ones. To demonstrate this, let's revisit the Little Lemon scenario, where customers are required to complete the use of registration form. Suppose that the form has multiple fields that must be checked and verified, in this case, the username field is mandatory and cannot be empty. To define a custom error, you can create an enumeration that conforms to an error protocol. This can be achieved by inserting the key word Error after the enumeration definition and before the enumeration implementation. Then inside the enumeration, you can define a case called usernameEmpty. Now, the error can be thrown inside a throwable function using the keyword throw. Let's explore the scenario further. Suppose you were tasked with writing a function that takes a user password as an argument, and then verifies that it's longer than six characters. In case it isn't, you would like to throw a custom error. Let's implement a throwable function to address the scenario. First, let's start by declaring a function called validatePassword. I insert a single password argument of type String. So far, the function doesn't do anything. In this scenario, the only current requirement is that the password should be more than six characters. So let's insert an if statement next. If the password is shorter than or equal to 6 characters, throw an error. Now, I need to define the custom error that represents the password being too short. First, I define an enumeration and call its PasswordValidationError. Then I insert the Error keyword to make the enum conform to an error protocol. Next, I define a case within the enumeration to represent a password being too short. For instance, passwordTooShort. Now that I have my custom error case defined, I can throw it in my validation function. Inside the if statement, I used the keyword throw, followed by the custom error. And that's it, I defined a function to throw an error if the password is too short. However, you can only throw errors inside the ThrowableFunctions. This means that I need to declare my function as a throwable. I do that by inserting the keyword throws after the function definition. Now, if I build the project, I can observe that there are no errors, and my password validation function is ready to be used. An advantage of using ThrowableFunctions in this situation is that I can extend the enumeration object with many more error cases if I get more requirements for password security later. In this case, I would only have to update the enumeration and add more if checks in the validation function. ThrowableFunctions play an integral part in Swift development as it's one of the built-in mechanisms to report issues that occurred while trying to execute a function. In this video, you have learned what ThrowableFunctions are in Swift. You now know that to make a function throwable, you have to use the throws keyword at the end of the function definition. You also discover that to throw an error inside the throwable function, you have to use another keyword throw, followed by an error. You also explore some examples of how to write a custom object that can be thrown.