iOS Error Handling in Swift Error Handling Throwing Errors

For this task, let's get some practice throwing an error. You've been provided with an error object in the editor. Error

For this task, let's get some practice throwing an error. You've been provided with an error object in the editor. Errors are typically thrown from inside functions so let's start by creating a function that can throw. Name this function lend. It takes no parameters and has no return type.

error.swift
enum BookError: Error {
  case incorrectTitle
  }

  func lend : throws { 
  case throw BookError.IncorrectTitle 
  }

1 Answer

Greg Kaleka
Greg Kaleka
38,985 Points

Hi there,

Sorry for the late reply - for future reference, it helps if you add some of your own words to your question about what you've tried, what's confusing you, etc. You're more likely to get responses from folks here in the community.

First of all, I really recommend you do some reviewing of how to write throwing functions. Your first problem is that you're not using the basic format correctly. Here's how it should look (this will pass the first challenge):

enum BookError: Error {
  case incorrectTitle
}

func lend() throws {

}

Apart from that, you don't need a case statement in your function lend() (case is only for switch statements and enumerations), and watch your capitalization!

Cheers :beers:

-Greg