Bird
0
0

Identify the error in this Swift code snippet:

medium📝 Debug Q6 of 15
iOS Swift - Swift Language Essentials
Identify the error in this Swift code snippet:
func readFile() throws -> String {
  // code
}
let content = readFile()
print(content)
AFunction should not be marked throws
BMissing catch block
CMissing try when calling throwing function
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check calling of throwing function

    Calling a function marked with throws requires try keyword.
  2. Step 2: Understand error handling requirements

    Without try, the code will not compile because error handling is missing.
  3. Final Answer:

    Missing try when calling throwing function -> Option C
  4. Quick Check:

    Calling throws function needs try = A [OK]
Quick Trick: Always use 'try' when calling throwing functions [OK]
Common Mistakes:
  • Calling without try
  • Expecting catch without do block
  • Removing throws from function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes