0
0
Swiftprogramming~5 mins

Throwing functions with throws in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the throws keyword mean in a Swift function?
It means the function can produce an error that must be handled by the caller using try, try?, or try!.
Click to reveal answer
beginner
How do you call a function that can throw an error?
You use the try keyword before the function call, and handle errors with do-catch or propagate them further.
Click to reveal answer
beginner
What is the purpose of the do-catch block in Swift?
It lets you run code that might throw errors and catch those errors to handle them safely.
Click to reveal answer
beginner
What happens if you call a throwing function without try?
The code will not compile because Swift requires explicit handling of possible errors.
Click to reveal answer
intermediate
Explain the difference between try? and try!.
try? converts the error into an optional value (nil if error), while try! forces the call and crashes if an error occurs.
Click to reveal answer
What keyword must a Swift function include if it can throw an error?
Acatch
Btry
Cthrows
Derror
How do you handle errors when calling a throwing function?
AUse <code>throws</code> when calling
BJust call the function normally
CUse <code>catch</code> without <code>try</code>
DUse <code>try</code> and <code>do-catch</code>
What does try? do when a throwing function fails?
AReturns nil instead of throwing
BCrashes the program
CIgnores the error silently
DThrows the error normally
What happens if you call a throwing function without try?
ACompilation error
BRuns normally
CRuntime error
DWarning but runs
Which block is used to catch errors thrown by a function?
Atry-catch
Bdo-catch
Cthrow-catch
Dif-else
Describe how to write and call a throwing function in Swift.
Think about marking the function and how the caller must handle errors.
You got /4 concepts.
    Explain the difference between try, try?, and try! when calling throwing functions.
    Consider how each handles errors differently.
    You got /3 concepts.