Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
39,019 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