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'

Double click on the error icon displayed against the fifth line.

[Solved] Swift - Call can throw, but is not marked with 'try'

Quick fix is to mark the function call with ‘try’ as shown below.

[Solved] Swift - Call can throw, but is not marked with 'try'

Its good to surround this statement with do-catch as shown below.

[Solved] Swift - Call can throw, but is not marked with 'try'

Conclusion

In this Swift Tutorial, we learned how to resolve the error “Call can throw, but is not marked with ‘try'” in Swift.