0
0
Swiftprogramming~10 mins

Throwing functions with throws in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Throwing functions with throws
Define function with 'throws'
Call function with 'try'
Function runs
No error
Return value
Continue
A function marked with 'throws' can produce an error. When calling it, use 'try' and handle errors with 'do-catch'.
Execution Sample
Swift
enum MyError: Error {
    case badInput
}

func checkNumber(_ num: Int) throws -> String {
    if num < 0 { throw MyError.badInput }
    return "Number is \(num)"
}
This function throws an error if the number is negative, otherwise returns a message.
Execution Table
StepActionInputConditionResultError ThrownOutput
1Call checkNumber with 555 < 0?FalseNo"Number is 5"
2Return from functionNo"Number is 5"
3Call checkNumber with -3-3-3 < 0?TrueYes: MyError.badInput
4Catch error in do-catchYes"Caught error: badInput"
5End
💡 Execution stops after handling error or returning value.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
num-5-3-3
errorThrownNoNoYesYes
output-"Number is 5""Caught error: badInput"
Key Moments - 3 Insights
Why do we need to use 'try' when calling a throwing function?
Because the function can throw an error, 'try' tells Swift to handle that possibility, as shown in execution_table step 1 and 3.
What happens if the function throws an error?
The error is passed to the 'catch' block in the 'do-catch' structure, as seen in execution_table step 4.
Can the function return a value if it throws an error?
No, if an error is thrown, the function stops and does not return a value, shown in step 3 where output is empty.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after calling checkNumber with 5?
A"Number is 5"
B"Caught error: badInput"
CNo output
DMyError.badInput
💡 Hint
Check row 1 and 2 in execution_table for output when input is 5.
At which step does the function throw an error?
AStep 4
BStep 1
CStep 3
DStep 2
💡 Hint
Look at the 'Error Thrown' column in execution_table.
If we remove 'try' when calling the throwing function, what happens?
AThe code compiles and runs normally
BCompilation error because 'try' is required
CThe function never throws an error
DThe error is automatically caught
💡 Hint
Swift requires 'try' to call throwing functions, as shown in concept_flow.
Concept Snapshot
Throwing functions use 'throws' to signal errors.
Call them with 'try' inside 'do-catch' blocks.
If error occurs, control jumps to 'catch'.
If no error, function returns normally.
Always handle errors to avoid crashes.
Full Transcript
In Swift, functions that can produce errors are marked with 'throws'. When you call these functions, you must use 'try' to acknowledge the possibility of an error. The call is usually inside a 'do-catch' block to handle errors gracefully. If the function runs without error, it returns a value. If it throws an error, the catch block runs. This prevents the program from crashing and lets you respond to problems. The execution table shows calling the function with a positive number returns a message, while calling it with a negative number throws an error that is caught and handled.