0
0
Swiftprogramming~10 mins

Do-try-catch execution flow in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Do-try-catch execution flow
Start
Enter do block
Execute try statement
Success?
YesContinue after do
Exit do-try-catch
Error thrown?
YesCatch matching error
Handle error
Exit do-try-catch
The program enters the do block, tries to run code that might throw an error, and if an error occurs, it jumps to the matching catch block to handle it.
Execution Sample
Swift
enum MyError: Error {
    case badInput
}

func throwError() throws {
    throw MyError.badInput
}

do {
    try throwError()
    print("Success")
} catch MyError.badInput {
    print("Caught bad input error")
}
This code tries to run a function that may throw an error and catches a specific error if thrown.
Execution Table
StepActionTry Statement ResultCatch Block EnteredOutput
1Enter do blockN/ANo
2Execute try throwError()Throws MyError.badInputYes, catch MyError.badInput
3Handle error in catchN/AYesCaught bad input error
4Exit do-try-catchN/ANo
💡 Error thrown in try, caught in catch, then execution exits do-try-catch block
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Output"""""Caught bad input error""Caught bad input error"
Key Moments - 2 Insights
Why does the code jump to the catch block instead of printing "Success"?
Because the try statement threw an error (Step 2 in execution_table), the normal flow is interrupted and the catch block runs to handle the error.
What happens if no error is thrown inside the do block?
If no error is thrown, the catch block is skipped and the code after try runs normally, like printing "Success" (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after Step 3?
A"Caught bad input error"
B"Success"
C"" (empty string)
DRuntime error
💡 Hint
Check the Output column at Step 3 in the execution_table
At which step does the catch block start executing?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the Catch Block Entered column in execution_table
If the try statement did not throw an error, what would happen?
ACatch block runs anyway
BProgram crashes
CCode after try runs, catch block skipped
DExecution stops immediately
💡 Hint
Refer to key_moments explanation about no error thrown
Concept Snapshot
do {
  try someFunction()
  // code if no error
} catch ErrorType {
  // handle error
}

- Code inside do runs first
- try runs code that may throw
- If error thrown, jump to matching catch
- If no error, catch skipped
- After catch or do, execution continues
Full Transcript
In Swift, the do-try-catch structure lets you run code that might cause an error. You start with do, then try to run code that can throw an error. If it throws, the program jumps to the catch block that matches the error type to handle it. If no error happens, the catch block is skipped and the code after try runs normally. This flow helps keep your program safe from crashes by managing errors clearly.