Swift – Call can throw, but is not marked with ‘try’
We get this error in Xcode when we try to make a call to a function that can throw an error, but we did not try to catch that error.
For example, in the following program, we try to get the contents of a Directory using contentsOfDirectory() function.
![[Solved] Swift - Call can throw, but is not marked with 'try'](https://www.tutorialkart.com/wp-content/uploads/2021/03/swift-call-can-throw.png)
Double click on the error icon displayed against the fifth line.
![[Solved] Swift - Call can throw, but is not marked with 'try'](https://www.tutorialkart.com/wp-content/uploads/2021/03/swift-call-can-throw-2.png)
Quick fix is to mark the function call with ‘try’ as shown below.
![[Solved] Swift - Call can throw, but is not marked with 'try'](https://www.tutorialkart.com/wp-content/uploads/2021/03/swift-call-can-throw-3.png)
Its good to surround this statement with do-catch as shown below.
![[Solved] Swift - Call can throw, but is not marked with 'try'](https://www.tutorialkart.com/wp-content/uploads/2021/03/swift-call-can-throw-4.png)
Conclusion
In this Swift Tutorial, we learned how to resolve the error “Call can throw, but is not marked with ‘try'” in Swift.