Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,021 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