NEW: Learn to build the incredible iOS 15 Weather app today! >>

SOLVED: Question: why do I have to throw Errors in this case ? (related to Day 8 CheckPoint4)

Forums > 100 Days of SwiftUI

I don't understand why throw error is necessary.

At the begining I thought throw error was a way to tell programmer that there were some coding written wrong. Then I realized it was actually just to display some information to the users, such as "your password is too short"

In Check Point 4 . Paul asked : If the number is less than 1 or greater than 10,000 you should throw an “out of bounds” error.

Why do I have to throw an error here ? I wrote the following lines and it achieved the goal of print "out of bounds" when the number is greater than 10,000

    func printX(x:Int){
    if x > 10_000 {
    print ( "out of bounds ")

Yes I understand CP4 is about the square root solution, which is not as simple as my codes passing x a value in such an easy way.

But my real question is more about why do we need to throw errors, when we can just simply print a string telling the same info?


FYI, here's the snippet regarding throwing error when it is too high than 10,000 in the solution provided by Paul .

    func squareRoot(of number: Int) throws -> Int {
if number < 1 {
    throw RootError.tooLow

if number > 10_000 {
    throw RootError.tooHigh

why can't we just say

   if number <1{
   print "out of bounds" }


In this case it maybe trivial, and as you point out you can write something simple, but this is also about learning and practicing throwing errors (when appropriate and useful for the code).

You can create your own error cases, that conform to the error protocol, or use do-catch, or try? to manage and handle errors (or potential errors) and avoid your app crashing out. There is a lot more, but you end up writing neater and easier to understand code. When you throw an error, you exit current loop or function, jump to handling the error, or propagate the error to higher levels, that will then handle it.


You may have answered your own question. In simple applications, you can send a message to the console via a print() statement. But if you want to sell this application, your user won't be able to see the message on their iPhone!

Throwing an error gives you a specific set of error messages that you can intercept, and respond appropriately to. For example, what do you DO if someone enters a postal code that doesn't have enought characters? What if the postal code is valid for England, but not for Germany?

In these cases, you might develop a number of enums that define different types of error codes. PostalCode.tooShort, PostalCode.tooLong, PostalCode.missingCountryCode, and more.

When you CATCH these thrown codes in your application, you can then perform extra logic to alert your user. Do you want to turn a field RED? Do you want to display an alert, play a sound ? Maybe you want to refetch data from your applications database.

What you do when you CATCH an error is the heart of your application's business logic. The THROW / CATCH pattern gives you a clean way to define typical, expected errors and the respond to them appropriately.


Great Answers, and thanks a lot ! @Greenamberred @Obelix

Yeah, I rewatched the solution and throw error episodes...I think it is about understanding the language is what we use to talk to the computer.

It is like <heading> has its own specific sizes in differnt categories. By using "throw error", I'm telling Swift that what this exactly is, and Swift may understand to get prepared to work on it in the future. If I only print a string, Swift won't condition it enough for future use.


Hi @boat

While you may not create many or any throwing functions early on, you will be using throwing function that Apple or other developer have done (Swift Package). So you need to be aware of them and how to handle them safely. (do, try, catch) etc.

So it really useful to know about them and what they can do. So if the problem come up, you will have it in your goody bag.


Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.