A SwiftUI student scratches her grey hair and ponders...
create function that takes an INT argument, with throws -> Int (not sure I understand that)
Kitchen Disaster
If you're cooking biscuits for breakfast, your intent is to serve some biscuits. But if your process screws up, you THROW
the burnt mess into the rubbish bin and send an apology note to your guests. The note might be one a half-dozen standard excuses: Runny Batter, Burned To Crisp, Not Enough Baking Powder, Hard as Rock, Unusal Consistency, Dropped on the Floor. You're the cook, you need to inform your guests with a good and proper reason.
Coding Disaster
Likewise, the signature of your function tells the programmer that your intention is to return an Int
. But if something goes wrong inside the function, you won't return an Int
. Instead you will THROW
an error, in the form of an enum
eration value specifying the type of error encountered.
Please make a note! Here in Checkpoint 4 you are throwing an enumeration value. But in future applications, you might throw
an object that has a much richer set of debugging data in the form of an error struct
.
Homework Reading Assignment
See 👉🏻Apple Error Documentation
Bad Coding Practice
In some languages lacking the ability to throw
an error, you might instead return a numeric error code. For example, you might return a -10 signifying your input was out of bounds. You might return a -42 meaning there is no valid result. Additionally, you might return a -75 to signify the calculation yields an imaginary number. You see the problem with this scheme? Don't do this in your code.
Keep Coding