do-try-catch versus Optional return types
error modeling choices.
throwing functions with do/try/catch convey why something failed via typed Error values; Optionals only say success or absence. Use throws when the caller needs the reason.
What's really being asked
It checks whether you model failure deliberately, matching the mechanism to whether the caller needs to know why something went wrong.
Do-try-catch
A function declared throws can throw a value conforming to Error. Callers prefix the call with try inside a do block and handle failures in catch clauses, which can pattern-match specific error cases. This carries semantic information: the error type and associated values explain the cause, so the caller can branch, retry, or surface a precise message. try? converts a throw to nil and try! asserts success.
Optional return
An Optional return type encodes only two states, a value or nil. It says something is absent but never says why. It is lightweight and ideal when absence is a normal, expected outcome rather than an error.
When each wins
Use throws when there are multiple meaningful failure reasons the caller should distinguish, such as network, decoding, or validation errors, or when you want errors to propagate up cleanly. Use Optional when the only question is whether a value exists, like a dictionary lookup, a failable initializer for an out-of-range input, or finding an element that may not be present, where nil is not exceptional.
The mistakes people make
Throwing for routine absence, which forces callers into do/catch for a non-error. Returning nil for genuine errors, discarding the reason and hiding bugs. Believing the two are interchangeable.
What usually comes next
What does try? do? When use Result instead? How do typed throws or rethrows fit in? How does this interact with async functions?
A concrete example
A JSON loader can fail because the file is missing, the bytes are malformed, or a field is invalid. Modeling it as throws lets the caller catch each case and show a targeted message or retry. By contrast a function that returns the first admin user in a list returns User? because having no admin is a perfectly normal result, not an error worth a thrown type.
Interview question
Which scenario most clearly favors a throwing function over an Optional return type?
- a.Looking up a key that may simply be absent from a dictionary
- b.An operation that can fail for several distinct reasons the caller must distinguish and react toCorrect
- c.Finding the first element in an array that may not exist
- d.A function that should never communicate any failure detail
Why? this is the answer
throws carries the reason for failure, which matters when callers must distinguish causes. Plain absence, as in dictionary or first-element lookups, is the natural fit for Optional.
Just read this? Test yourself on what you have been reading.
- #swift
- #error-handling
- #optionals
- #do-catch
- #throws
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on swift — each one lists the topics its interview covers.
See open roles