Daniel's working notes

Throwing function

  • From general perspective, it’s one of Error handling in Swift. In Swift, error is represented by value type (structs, or enum) and conform to the Error protocol.
  • To mark a function that can throw an error, you mark it with throws keyword before the returned value
    func canThrowErrors() throws -> String
    func cannotThrowErrors() -> String
    
  • ::When you don’t provide throws, you must handle error inside the function itself::
  • When calling function that throws error, you should use do catch block and using try
  • When execution is produced an error, it will moved to the catch block
  • When you create function that throws error, you can use guard let keyword to return early with error specified
  • Sometimes, you don’t want to handle all the error case, for this you can use try? that produces nil when error

References

Linked Notes: